2014-10-09 149 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