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


当前回答

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

其他回答

你可以使用.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>

对于jQuery版本< 1.9:

$('#inputId').attr('disabled', true);

对于jQuery版本>= 1.9:

$('#inputId').prop('disabled', true);

检查下面的代码:

<input id="mail">

<script>

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

</script>

jQuery < 1.9

$('#inputId').attr('readonly', true);

jQuery 1 + 9。

$('#inputId').prop('readonly', true);

阅读更多关于prop和attr的区别