简体   繁体   中英

Values of two select boxes

Briefly Explaining my program :

I Have 3 select boxes

If the value selected by the user in the 1st box is 2 & Value selected by the user in second box is 3 (option values) then the third select box should display an array.

This code doesn't work but shows an idea:

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)
}

I need help for the code included in if statment and && operation for checking the expression.

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) {
        // ...
    }
}

assuming

<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>

script:

$('#secondbox, #firstbox').on('change',function(){
  if($('#firstbox').val() == 2 && $('#secondbox').val() == 3)
    alert('array display code here...');
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM