[英]Swap values of two select boxes in select2 plugin Javascript or jquery
[英]Values of two select boxes
简要说明我的程序:
我有3个选择框
如果用户在第一个框中选择的值为2且用户在第二个框中选择的值为3(选项值),则第三个选择框应显示一个数组。
这段代码不起作用,但是显示了一个想法:
if ($('#firstbox').click(function() { ($(this).val() == '2'); } &&
$('#secondbox').click(function() { ($(this).val() == '3'); }) {
// Array Display here (not included the coding here, because there is no issue with this coding.. seems working fine)
}
我需要if语句和&&操作中包含的代码来检查表达式。
var select1 = $('#firstbox');
var select2 = $('#secondbox');
$([select1, select2]).change(function () {
if ( select1.val() == 2 && select2.val() == 3 ) {
// run your code here...
}
});
您需要将click事件绑定到第三个选择框,或者绑定到前两个选择框的change事件,然后回调一个公共函数来更新第三个选择框。
$("#firstbox, #secondbox").change(UpdateThird);
function UpdateThird() {
var a = $("#firstbox").val();
var b = $("#secondbox").val();
if (a == 2 && b == 3) {
// ...
}
}
假设
<select id="firstbox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="secondbox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
脚本:
$('#secondbox, #firstbox').on('change',function(){
if($('#firstbox').val() == 2 && $('#secondbox').val() == 3)
alert('array display code here...');
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.