我试图在“如果用户选择[x]选项,将它们发送到[x]页面”逻辑之后,设置表单提交上的条件页面加载。我对JavaScript并不是很熟悉,但这似乎是最简单的方法。
这是我的HTML:
<form id="page1" action="javascript:OpenWindow()" method="post">
<fieldset id="mainSelection">
<label><input type="radio" class="radio-button" value="A" name="sel1"> text</label><br />
<label><input type="radio" class="radio-button" value="B" name="sel1"> text</label><br />
<label><input type="radio" class="radio-button" value="C" name="sel1"> text</label><br />
<label><input type="radio" class="radio-button" value="D" name="sel1"> text</label><br />
</fieldset>
<button type="submit" value="Next" id="submitButton">Next</button>
</form>
这是我的脚本:
<script type="text/javascript" language="javascript">
function OpenWindow() {
var choice = document.getElementByClassName("radio-button").value
if (choice == A) {
window.open('http://www.website.net');
}
else if (choice == B) {
window.open('http://www.website.net');
}
else if (choice == C) {
window.open('http://www.website.net');
}
else {
window.open('http://www.website.net');
}
}
</script>
我在这里做什么不正确?
最佳答案
我进行了一些更改。
将onsubmit =“ OpenWindow()”添加到表单元素
更改获取所选元素的方式
有条件的引号(“ A”,“ B”等)
这是代码笔的链接:http://codepen.io/anon/pen/qajxh
所以看起来像这样:
<form onsubmit="OpenWindow()" id="page1" action="javascript:OpenWindow()" method="post">
<fieldset id="mainSelection">
<label><input type="radio" class="radio-button" value="A" name="sel1"> text</label><br />
<label><input type="radio" class="radio-button" value="B" name="sel1"> text</label><br />
<label><input type="radio" class="radio-button" value="C" name="sel1"> text</label><br />
<label><input type="radio" class="radio-button" value="D" name="sel1"> text</label><br />
</fieldset>
<button type="submit" value="Next" id="submitButton">Next</button>
</form>
使用这样的js:
function OpenWindow() {
var choice = document.getElementById("page1");
choice = choice.sel1.value;
if (choice == 'A') {
window.open('http://www.website.net');
}
else if (choice == 'B') {
window.open('http://www.website.net');
}
else if (choice == 'C') {
window.open('http://www.website.net');
}
else {
window.open('http://www.website.net');
}
}
关于javascript - 使用JavaScript有条件地打开表单提交页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24458634/