使用Javascript单击按钮时,根据选项选择重定向到页面



我正在处理一个表单,该表单检查选中了<select>元素中的哪个选项,然后在单击按钮后重定向到页面。

我已经尝试过从jQuery获取select onChange的值,但只有当选项立即被选中时,它才有帮助。

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.getElementById('payment-select')

function getval() {
var sel = document.getElementById('payment-select');
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">

最新更新