我正在尝试一个下拉选择,更改表单操作,这将使用户在提交时指向另一个链接。
我已经尝试了我认为应该起作用的东西,但是没有办法,我在这里所做的任何错误的帮助都会受到极大的感谢。
js
$("#time-select").change(function() {
if (document.getElementById("time-select").value != "WEEK")
{
document.getElementById("subs-form").setAttribute("action", "A");
}
else (document.getElementById("time-select").value != "FORTNIGHT")
{
document.getElementById("subs-form").setAttribute("action", "B" );
}
else (document.getElementById("time-select").value != "MONTH")
{
document.getElementById("subs-form").setAttribute("action", "C" );
}
}html
<div class="wrapper">
<form name="linkForm" id="subs-form" action="" method="GET" >
<select id="time-select" >
<option value="WEEK" selected>1 BAG A WEEK</option>
<option value="FORTNIGHT">1 BAG A FORTNIGHT</option>
<option value="MONTH">1 BAG A MONTH</option>
</select>
<input id="subs-submit" type="submit" value="Subscibe">
</form>
</div>发布于 2015-10-08 12:32:58
尽可能保持代码干净(例如使用jQuery选择器),请参见下面的示例:
$("#time-select").change(function() {
var value = $(this).val(),
action = null;
switch(value) {
case "WEEK":
action = "A"
break;
case "FORTNIGHT":
action = "B"
break;
case "MONTH":
action = "A"
break;
}
$("#subs-form").attr("action", action);
}发布于 2015-10-08 12:44:13
$("#time-select").change(function() {
var option = document.getElementById("time-select").value;
var el = document.getElementById("subs-form");
switch(option) {
case "WEEK":
action = "A"
break;
case "FORTNIGHT":
action = "B"
break;
case "MONTH":
action = "C"
break;
}
el.setAttribute("action", action);
});发布于 2015-10-08 13:38:05
$("#time-select").on('change', function() {
var value = $(this).val();
if (value !== "WEEK")
document.getElementById("subs-form").setAttribute("action", "A");
else if (value !== "FORTNIGHT")
document.getElementById("subs-form").setAttribute("action", "B" )
else if (value !== "MONTH")
document.getElementById("subs-form").setAttribute("action", "C" );
});https://stackoverflow.com/questions/33015659
复制相似问题