[英]Submit form on change of dropdown only when special value is selected
I have a dropdown list with a few options (eg 7). 我有一个带有几个选项的下拉列表(例如7)。 The form should be submitted when one of the last 3 options is selected. 选择最后三个选项之一时,应提交表单。 On the first 4 options the form should not be submitted. 在前四个选项中,不应提交表格。
onChange="this.form.submit()"
does not help - the form will be submitted on every change... onChange="this.form.submit()"
没有帮助-每次更改都会提交表单...
Any ideas? 有任何想法吗?
Thanks a lot in advance 提前谢谢
Hias 希亚斯
Do... 做...
onchange="checkVal();"
Then in your JS make the function... 然后在您的JS中创建函数...
function checkVal{
var e = document.getElementById("FORMID");
var selectedVal = e.options[e.selectedIndex].value;
if (selectedVal == val4 || selectedVal == val5 || selectedVal == val6){
e.submit();
}
}
This will work if you have your options set up with values <option val="4">number 4</option>
If not you have to use .text
instead of .value
to get the selectedVal
and then compare the text inside the options 如果您将选项设置为值<option val="4">number 4</option>
这将起作用,否则,您必须使用.text
而不是.value
来获取selectedVal
,然后比较选项内的文本
It's simple check the value of select element if it's what you want then tell form to submit itself. 简单检查一下select元素的值是否是您想要的,然后告诉表单提交自己。
Demo of this : http://jsfiddle.net/Q6FUM/ 演示 : http : //jsfiddle.net/Q6FUM/
var f = document.forms['mf'], s= document.getElementById('ss');
s.onchange = function() {
if (s.value==3) f.submit();
}
Thanks a lot. 非常感谢。 It works with this code: 可以使用以下代码:
function checkVal() {
var f = document.getElementById("FORM-ID");
var e = document.getElementById("SELECT-ID");
var selectedVal = e.options[e.selectedIndex].value;
if (selectedVal > 4) {
f.submit();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.