仅将禁用应用于一个输入按钮

时间:2015-03-31 09:41:47

标签: javascript jquery html asp.net input

如果仅在第二个按钮上单击复选框,我如何使用'id'仅应用禁用/启用按钮,因为我需要根据是否单击复选框来打开和关闭它。第一个按钮始终在哪里

http://jsfiddle.net/BPhZe/2122/

    <h1>Button should be enabled if at least one checkbox is checked</h1>

<form>
    <div>
        <input type="checkbox" name="option-1" id="option-1"> <label for="option-1">Option 1</label>
    </div>
    <div>
        <input type="checkbox" name="option-2" id="option-2"> <label for="option-2">Option 2</label>
    </div>
    <div>
        <input type="submit" id="sub1" value="Do thing">
    </div>

    <div>
        <input type="checkbox" name="here" id="option-12"> <label for="option-2">Option 3</label>
    </div>
    <div>
        <input type="checkbox" name="now" id="option-22"> <label for="option-2">Option 4</label>
    </div>

    <div>
        <input type="submit" id="sub2" value="Do thing" disabled="true">
    </div>
</form>

var checkboxes = $("input[type='checkbox']"),
    submitButt = $("input[type='submit']");

checkboxes.click(function() {
    submitButt.attr("disabled", !checkboxes.is(":checked"));
});

2 个答案:

答案 0 :(得分:3)

var checkboxes = $("input[type='checkbox']"),
    submitButt2 = $("#sub2");

checkboxes.click(function() {
    submitButt2.attr("disabled", !checkboxes.is(":checked"));
});

答案 1 :(得分:1)

使用第二个提交按钮的id代替input[type='submit']

var submitButt = $("#sub2");

FIDDLE DEMO