禁用复选框和下拉列表

时间:2013-05-03 16:05:50

标签: jquery html forms

我正在为我的网站开发一个日历应用程序,而且我有点腌菜。这是我需要帮助的应用程序的一部分的html:

<table border="0" cellpadding="2" cellspacing="4">
<tbody>
<tr>
<td class="main b_width"><strong>Repeat Every:</strong></td>
<td class="main width">

<select name="Day">

<option value="1" selected>no recurrence</option>
<option value="2">days</option>
<option value="3">weeks</option>
<option value="4">month by day</option>
<option value="5">month by week</option>

</select>

</td>
</tr>

<tr>
<td class="main b_width"><strong>At Days:</strong></td>
<td class="main b_width">

<select name="Day">

    <option value="1" selected>First</option>
    <option value="2">Second</option>
    <option value="3">Third</option>
    <option value="4">Fourth</option>

</select>

<input type="checkbox" name="Monday" ;>&nbsp;Mon&nbsp;

 </td>
</tr>

我想知道的是,创建的第一个选择框,对于第一个选项'不再发生',我如何使所有其他选择框和复选框处于非活动状态,禁用它们以便不能选择任何数据,任何帮助将不胜感激

4 个答案:

答案 0 :(得分:1)

试试这个:

$('select[name=Day]').eq(0).change(function () {
    $('select[name=Day]').eq(1).prop('disabled', (this.value === '1'));
    $('input[name=Monday]').prop('disabled', (this.value === '1'));
}).change();

FIDDLE

答案 1 :(得分:0)

$('table :input:gt(0)').prop('disabled', true);
$('#recur').change(function () {
    $('table :input:gt(0)').prop('disabled', ($(this).val() == 1));
});

<强> jsFiddle example

您的示例的一个问题是您有多个具有相同名称的选择元素。我更新了您的HTML,为他们提供了唯一的ID。请参阅HTML的小提琴。

答案 2 :(得分:0)

尝试这种方式:

$("#Day").change(function(e){
        if($(this).val()=='1'){
            $("#Day2").attr("disabled","disabled");
        }
    });

答案 3 :(得分:0)

你需要使用javascript来实现这个目标 将ID recurrence添加到您的第一个选择框,将类recurrence-input添加到您希望在未选择重复时禁用的每个元素,并使用以下javascript(使用jquery):

$('#recurrence').on('change', function() {} {
    if ($(this).val() === '1') {
        $('.recurrence-input').attr('disabled', 'disabled');
    } else {
        $('.recurrence-input').removeAttr('disabled');
    }
});

如果no-recurrence是您选择框的默认值,那么您也应该在加载文档时禁用其他输入。