簡體   English   中英

兩個選擇框的值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM