我如何添加只读到特定的<输入>?.attr('readonly')不起作用。


当前回答

你可以使用.removeAttr;

$('#descrip').removeAttr('readonly');

其他回答

使用setAttribute属性。注意,如果选择1,则在文本框上应用readonly属性,否则将删除readonly属性。

http://jsfiddle.net/baqxz7ym/2/

document.getElementById("box1").onchange = function(){
  if(document.getElementById("box1").value == 1) {
    document.getElementById("codigo").setAttribute("readonly", true);
  } else {
    document.getElementById("codigo").removeAttribute("readonly");
  }
};

<input type="text" name="codigo" id="codigo"/>

<select id="box1">
<option value="0" >0</option>
<option value="1" >1</option>
<option value="2" >2</option>
</select>

.attr('readonly', 'readonly')应该可以做到。你的.attr('readonly')只返回值,不设置值。

检查下面的代码:

<input id="mail">

<script>

 document.getElementById('mail').readOnly = true; // makes input readonline
 document.getElementById('mail').readOnly = false; // makes input writeable again

</script>

我认为“disabled”排除了POST上发送的输入

启用只读功能:

$("#descrip").attr("readonly","true");

禁用只读

$("#descrip").attr("readonly","");