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

问题描述 投票:0回答:1

我正在处理一个表单,该表单检查<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">

javascript jquery html
1个回答
-1
投票

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">
© www.soinside.com 2019 - 2024. All rights reserved.