如何根据某些选择启用文本框

时间:2013-03-07 15:36:56

标签: jquery html

仅在就业类型为合同或全职时启用txtEmployer,应停用全职。

<div>
  <select id="SelectEmployment">
    <option value="Select Employment">Employment Type</option>
    <option value="Full-time">Full-time</option>
    <option value="Part-time">Part-time</option>
    <option value="Contract">Contract</option>
    <option value="Fixed term">Fixed term</option>
  </select>
</div>
<div>
  <input type="text" id="txtEmployer" value="Employer" class="txtinline water"/>
</div>

4 个答案:

答案 0 :(得分:0)

这个怎么样?

$("#SelectEmployment").change(function () {
    if ($(this).val() == "Fixed term") {
        $('#txtEmployer').attr("disabled", "disabled");
    }
    else {
        $('#txtEmployer').removeAttr("disabled");
    }
});

答案 1 :(得分:0)

尝试以下代码:

$("#SelectEmployment").change(function () {
    if ($(":selected", $(this)).text() == "Contract" || $(":selected", $(this)).text() == "Full-time") {
        $("#txtEmployer").removeAttr("disabled");
    } else {
        $("#txtEmployer").attr("disabled", "disabled");
    }
});

答案 2 :(得分:0)

这样的东西
function updateState(val){
    $('#txtEmployer').prop('disabled', !(val == 'Full-time' || val == 'Contract'));
}

$('#SelectEmployment').change(function(){
    var val = $(this).val();
    updateState(val);
});

updateState($('#SelectEmployment').val());

演示:Fiddle

答案 3 :(得分:0)

关于全职的问题是模棱两可的。所以,我假设第一个是兼职。试试这个:

$(document).ready(function(){
    $('#SelectEmployment').change(function(){
        if($(this).val() == 'Contract' || $(this).val() == 'Part-time') {
            $('#txtEmployer').removeAttr('disabled');
        }
        if($(this).val() == 'Full-time') {
            $('#txtEmployer').attr('disabled', true);
        }
    });
});