我正在处理一个表单,该表单检查<select>
元素中的哪个选项被选中,然后重定向到页面但仅在此后单击按钮时。这意味着当更改选择时,我不需要立即重定向,而在单击按钮后,不需要重定向。
我已经尝试过this question,但是仅当选择立即选项时,它才有帮助。
function getval() {
if (sel.value == "2") {
window.location.href = "payment1.html";
} else if (sel.value == "3") {
window.location.href = "payment2.html";
}
}
<select id="payment-select">
<option value="1">Select payment option</option>
<option value="2">Payment1</option>
<option value="3">Payment2</option>
</select>
<br>
<input type="button" onclick="getval()" class="button" value="Continue">
我如何将此脚本连接到我的<input type="button" onclick="getval()" class="button" value="Continue">
?
sel
变量是undefined
。您需要存储所选选项的值。您可以像这样选择它:document.querySelector('#payment-select')
。
function getval()
{
var sel = document.querySelector('#payment-select');
console.log('teste')
if (sel.value == "2") {
window.location.href = "payment1.html";
}
else if (sel.value == "3") {
window.location.href = "payment2.html";
}
}
<select id="payment-select">
<option value="1">Select payment option</option>
<option value="2">Payment1</option>
<option value="3">Payment2</option>
</select>
<br>
<input type="button" onclick="getval()" class="button" value="Continue">