在jQuery中为输入字段添加只读



我在添加readonly到一些输入字段时遇到问题。

我在<head>

中有这个代码
$(document).ready(function() {
    $('#powermail_field_ordreid').prop('readonly', true);
    $('#powermail_field_destination').prop('readonly', true);
});

然后我有6个输入字段中的这2个,我需要添加readonly

<div id="powermail_fieldwrap_9" class="powermail_fieldwrap powermail_fieldwrap_input powermail_fieldwrap_9 ">
    <label class="powermail_label" for="powermail_field_ordreid"> Ordre id </label>
    <input id="powermail_field_ordreid" class="powermail_field powermail_input " type="text" value="0521104821">
</div>
<div id="powermail_fieldwrap_7" class="powermail_fieldwrap powermail_fieldwrap_input powermail_fieldwrap_7 ">
    <label class="powermail_label" for="powermail_field_destination"> Destination </label>
    <input id="powermail_field_destination" class="powermail_field powermail_input " type="text" value="Luxembourg">
</div>

我已经尝试了.attr(),现在我使用.prop(),因为我使用jQuery 1.10+,但我仍然可以编辑输入字段,有什么问题。

编辑…可以看到它的工作很好,在jsfiddel上-所以谁能告诉我如果它是一个其他的脚本阻止它在这里http://wnf.dk/bestillingsform.html?tx_powermail_pi1[field][7]=Luxembourg前2个字段有readonly add.

您的web控制台日志:Uncaught ReferenceError: $ is not defined

你应该把对jquery脚本的引用放在前面。未捕获的ReferenceError: $未定义?

<script>
    $(document).ready(function(){
        $('#powermail_field_ordreid').attr('readonly', true);
        $('#powermail_field_destination').attr('readonly', true);
    });
</script>

它适用于jQuery <1.9

jQuery 1.9 +

<script>
        $(document).ready(function(){
            $('#powermail_field_ordreid').prop('readonly', true);
            $('#powermail_field_destination').prop('readonly', true);
        });
    </script>

你可以看看http://jsfiddle.net/najibcse/M7rkB/

最新更新