使字段只读 - 复选框 - jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7419383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:50:18  来源:igfitidea点击:

Make field readonly - checkbox - jQuery

jqueryhtmlcheckbox

提问by Oliver 'Oli' Jensen

I have this checkbox:

我有这个复选框:

<input type="checkbox" name="suspended" id="s" value="<?php echo $udata['suspended']; ?>" <?php echo $suspended; ?>>

And this textarea:

而这个文本区域:

<textarea name="suspendreason" id="sr" style="height:60px" class="field"><?php echo $udata['suspendreason']; ?></textarea>

My question is, how can I make the textarea READONLY whenever the #s checkbox is unchecked?

我的问题是,如何在未选中#s 复选框时使 textarea 只读?

回答by cillierscharl

With your requirement, something like this:

根据您的要求,如下所示:

$(document).ready(function(){
    if($('#s:checked').length){
        $('#sr').attr('readonly',true); // On Load, should it be read only?
    }

    $('#s').change(function(){
        if($('#s:checked').length){
            $('#sr').attr('readonly',true); //If checked - Read only
        }else{
            $('#sr').attr('readonly',false);//Not Checked - Normal
        }
    });
});

回答by Karyna Kerin

I recommend making it readonly and disabled.

我建议将其设置为只读和禁用。

$("#sr").attr('disabled', 'disabled');
$("#sr").attr('readonly', 'readonly');

simply because if someone is in Carat Browsing mode, rare, but yes I've had clients do this, they can edit the disabled fields.

仅仅是因为如果有人处于 Carat 浏览模式,这种情况很少见,但是我已经让客户这样做了,他们可以编辑禁用的字段。

And for the checkbox:

对于复选框:

<script type="text/javascript">
    $(document).ready(function () {
        // check upon loading
        if(!$("#s").is(:checked))
        {
           $("#sr").attr('disabled', 'disabled');
           $("#sr").attr('readonly', 'readonly');
        }

        // event
        $("#s").change(function () {
           if(!$(this).is(:checked)) {
           {
              $("#sr").attr('disabled', 'disabled');
              $("#sr").attr('readonly', 'readonly');
           }
           else
           {
              $("#sr").attr('disabled', '');
              $("#sr").attr('readonly', '');
           }
         }
      });
</script>

回答by ipr101

$(document).ready(function() {
    $("#s").change(function() {
        if (!this.checked) {
            $("#sr").attr('disabled', 'disabled');
            $("#sr").attr('readonly', 'true');
        }
        else {
            $("#sr").removeAttr('disabled');
            $("#sr").removeAttr('readonly');
        }
    });

    //triger change event in case checkbox checked when user accessed page
    $("#s").trigger("change")
});

Working demo - http://jsfiddle.net/cc5T3/1/

工作演示 - http://jsfiddle.net/cc5T3/1/

回答by Jorge Zapata

$("input[type=checkbox][checked]").each( 
    function() { 
      $("#sr").attr("disabled", "disabled")
    } 
);

回答by simoncereska

You can make it disabled = "disabled"

您可以将其禁用 = "disabled"