【问题标题】:Removing attribute disabled on a select menu when a specific box is checked选中特定框时删除选择菜单上禁用的属性
【发布时间】:2016-01-13 23:45:19
【问题描述】:

我尝试从选择菜单中删除禁用属性的代码块将我的复选框验证返回为真。如何正确删除 disabled 属性而不弄乱我的验证?

while($row = mysqli_fetch_assoc($query)){
echo "<h5><input class='check' panel-menu='menu$index'
type='checkbox' name='roomType[]' id='roomType[$index]' 
value='".$row['roomDetailsNo']."'> Choose this Room";

echo "<br>Number of Rooms:&nbsp;";
                echo "<select id='menu$index' name='noOfRooms[".$row['roomDetailsNo']."][]' disabled>";
                $rooms = 0;
                while($rooms <= $row['available_rooms']){
                echo "<option>";
                echo $rooms;
                echo "</option>";
                $rooms++;
                }
                echo "</select><br>";
}

这是我的 jquery

<script>

$(function(){
    $('#btn').on('click', function(){
        var check = $("input:checked").length;
        if(check <= 0){
            alert("Please Choose a Room");
            return false;
        }
        else{
            $('.check').on('click', function{
            var check = $(this).attr('panel-menu');
            checkbox = $('#'+check).is(':checked');
                if(checkbox){
                    $('#'+check).prop('disabled', false);
                    alert("checked");
                }
            });
            return true;
        }
    });
});

</script>

【问题讨论】:

标签: php jquery


【解决方案1】:

在这种情况下,请勿将一个 click 事件处理程序包含在另一个 click 事件处理程序中。
分别验证 #btn.check 的点击:

$('#btn').on('click', function(){
    var check = $("input:checked").length;
    if(check <= 0){
        alert("Please Choose a Room");
        return false;
    }
});

// you missing parenthesis for function "()":
$('.check').on('click', function(){
    // use data-* attributes (explanation below the code)
    var check = $(this).attr('data-panel-menu');
    if($(this).is(':checked')){
        $('#'+check).prop('disabled', 0);
    }else{
        // if checkbox isn't checked, set the first option as default:
        $('#'+check).prop('disabled', 1).children(':first').prop('selected', 1);
    }
});

JSFiddle demo

您应该使用data-* 属性,而不是自定义panel-menu 属性。 See why.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2016-09-27
    • 2012-02-26
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多