2014-10-09 152 views
-4

我想知道我怎麼可以禁用和啓用選項值複選框基地: 這是我的代碼:如何禁用/啓用選擇(選項)值複選框基地

<select id="typeofworkday"> 
    <option value="lavorato" id="lavorato" name="lavorato"> lavorato </option> 
    <option value="ferie" id="ferie" name="ferie"> ferie </option> 
    <option value="malattia" id="malattia" name="malattia"> malattia </option> 

<input type="checkbox" name="permesso" id="check1" disabled="disabled" onclick="check();"> Permesso 

如果用戶選擇(lavorato)複選框==啓用其他禁用。

+2

你不知道哪部分該怎麼辦?你卡在哪裏?顯示你到目前爲止。 – 2014-10-09 15:40:01

回答

0

所有你需要做的就是聽選擇,當它的變化作出反應。

// Listen to the change of the <select> element 
$("#typeofworkday").on("change", function() { 
    // Check the option value for "ferie" and disable the checkbox 
    if ($(this).val() === "ferie") { 
     $("#check1").attr("disabled", "disabled"); 

    // For all other options, enable the checkbox 
    } else { 
     $("#check1").removeAttr("disabled"); 
    } 
}); 

Here's a JSFiddle of it working

+0

Thanks它的工作。 – Sattar 2014-10-10 08:26:18

0

試試這個,

$('#typeofworkday').on('change', function() { 
    if ($(this).val() === 'lavorato') { 
     $('#check1').prop("disabled", false); 
    } else { 
     $('#check1').prop("disabled", true); 
    } 
}); 
+2

只是一個註釋......如果您只是將等同比較的結果用作'.prop()'的第二個參數,那麼您的代碼將更清晰。 *(儘管它需要是一個「不等於」的比較。)*'$('#check1')。prop(「disabled」,$(this).val()!=='lavorato')' – 2014-10-09 15:47:21

相關問題