[英]How to check when two check boxes both have nonempty values with jQuery
So I'm trying to make it so that when a user clicks one of the options, nothing happens, but the second they click select an option on the other selector (whose value isn't empty), the page will instantly submit. 因此,我试图做到这一点,以便当用户单击其中一个选项时,什么也没有发生,但是第二次单击时,单击另一个选择器上的一个选项(其值不为空),该页面将立即提交。 I know I could use a submit button, but I really dislike those.
我知道我可以使用“提交”按钮,但是我真的不喜欢这些按钮。
Does anyone know how to fix this? 有谁知道如何解决这一问题?
Here's a small hypothetical situation: 这是一个小的假设情况:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<select name="select" id="select">
<option value=''>--- Select ---</option>";
<option value=1>a</option>";
<option value=2>b</option>";
<option value=3>c</option>";
</select>
<select name="select2" id="select2">
<option value=''>--- Select ---</option>";
<option value=1>a</option>";
<option value=2>b</option>";
<option value=3>c</option>";
</select>
Here's my js for it: 这是我的js:
$(document).ready(function () {
if( $('#select').val() && $('#select2').val()) {
alert("aa");
submit();
}
});
Also, here's a jfiddle for convenience: https://jsfiddle.net/bcknhe36/1/ 另外,为方便起见,这里有一个jfiddle: https ://jsfiddle.net/bcknhe36/1/
You are only checking on page load 您只检查页面加载
You need to check within a change event handler after user has selected 用户选择后,您需要在更改事件处理程序中检查
$(document).ready(function() {
$('#select,#select2').on('change', function() {
if ($('#select').val() && $('#select2').val()) {
alert("aa");
$('#formID').submit();
}
});
});
Rather than checking on document ready you need to check on change. 无需检查文档是否准备就绪,您需要检查更改。 So when the drop down is changed check if both have a value.
因此,当下拉列表更改时,检查两者是否都具有值。
Updated fiddle : https://jsfiddle.net/bcknhe36/4/ 更新的小提琴: https : //jsfiddle.net/bcknhe36/4/
$(document).ready(function() {
$( "#select,#select2" ).change(function() {
if ($('#select').val() && $('#select2').val()) {
alert("aa");
submit();
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.