jQuery:如果checkboxes和dropdown选项相同,那么

时间:2011-11-18 12:52:53

标签: jquery forms

我有3个带有以下jQuery的复选框,效果很好。

$(document).ready(function() {
    var $finishBoxes = $('#co1,#co2,#co3');
    $finishBoxes.change(function(){
        // check if all are checked based on if the number of checkboxes total
        // is equal to the number of checkboxes checked
        if ($finishBoxes.length == $finishBoxes.filter(':checked').length) {
            $('#submit2pd').attr("disabled", false);
        }
        else{
            $('#submit2pd').attr("disabled", true);
        }
    });

    $finishBoxes.ready(function(){
        // check if all are checked based on if the number of checkboxes total
        // is equal to the number of checkboxes checked
        if ($finishBoxes.length == $finishBoxes.filter(':checked').length) {
            $('#submit2pd').attr("disabled", false);
        } 
        else{
            $('#submit2pd').attr("disabled", true);
        }
    });
});

我想在if语句中添加以下select选项,所以如果选中所有3并且复选框值= 1(是),那么$('#submit2pd')。attr(“disabled”,true); < / p>

<select name="tcaccept" id="tcaccept"> 
    <option value="0"<?php if($mychecklist->tcaccept==0) echo 'selected' ?>>No</option>
    <option value="1"<?php if($mychecklist->tcaccept==1) echo 'selected' ?>>Yes</option>
</select>

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

这里有一些正确的代码:

if ( ( $finishBoxes.length == $finishBoxes.filter(':checked').length ) && ( $('#tcaccept').val() == '1' )) {

    $('#submit2pd').prop("disabled", false);
    // $('#submit2pd').attr("disabled", "");


}else{

    $('#submit2pd').prop("disabled", true);
    //$('#submit2pd').attr("disabled", "disabled");
}

如果您使用的是jquery 1.6+,最好将.prop()用于特殊属性,例如禁用(或已检查 ...)。


根据评论

修改

<form>
    <input type="checkbox" id="col1" /><label for="col1">Checkbox 1</label>
    <input type="checkbox" id="col2" /><label for="col2">Checkbox 2</label>
    <input type="checkbox" id="col3" /><label for="col3">Checkbox 3</label>
    <select id="tcaccept">
        <option value="0">No</option>
        <option value="1">Yes</option>
    </select>
    <input type="submit" text="Submit" id="SubmitButton" />
</form>

要取消表单提交,只需return false;

$('#SubmitButton').click(function(event) {
    var $finishBoxes = $('#co1,#co2,#co3');

    if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && $('#tcaccept').val() == '1' )) {
        alert('Check all the boxes and select "Yes"');
        return false;
    }

    // otherwise the form is submitted
    alert('Form will be submitted');

});

我为你创建了一个jsfiddle

答案 1 :(得分:0)

尝试

$('#tcaccept option:selected').val() == "1"

另外,仅为您提供信息,我认为您不需要调用$ finishBoxes.ready,因为您已经在jquery ready方法中,因此应该准备好DOM。要删除代码重复,我会将更改调用的内容导出到外部函数:

$(document).ready(function() {
    var $finishBoxes = $('#co1,#co2,#co3');
    function onchange() {
        // check if all are checked based on if the number of checkboxes total
        // is equal to the number of checkboxes checked
        if ($finishBoxes.length == $finishBoxes.filter(':checked').length) {
            $('#submit2pd').attr("disabled", false);
        }
        else{
            $('#submit2pd').attr("disabled", true);
        }
    }
    $finishBoxes.change(onchange);
    // Call once to for the first load
    onchange()
});