使用 jQuery 使输入文本在 3 秒内再次可编辑之前不可编辑

Making input text uneditable before making it editable again in 3 seconds using jQuery

我有一个输入文本,当您按回车键时,它应该使输入文本不可编辑,然后在 3 秒后再次使其可编辑。但由于某种原因它不起作用。我究竟做错了什么?谢谢

<input type = "text" id = "text">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type = "text/javascript">

$(function(){

$("#text").keyup(function(e){ 
    if (e.which == 13) {
    $("#text").prop("readonly", true);
    setTimeout(function(){
    $("#text").prop("readonly", false);
}, 3000);

}); //end of if (e.which == 13)

}); //end of $(function())

</script>

if closing 中的简单拼写错误,有一个额外的括号将其删除

$(function() {
  $("#text").keyup(function(e) {
    if (e.which == 13) {
      $("#text").prop("readonly", true);
      setTimeout(function() {
        $("#text").prop("readonly", false);
      }, 3000);
    } //end of if (e.which == 13)
  //-^--- remove ) from here
  }); //end of $(function())
});
// also add this closing
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="text">

取决于您的 jquery 版本,以下选项将起作用:

 $("#text").attr('readonly', true); //this work for older jquery version 

 $("#text").prop('readonly', true);  //for new jquery version

要删除只读属性,您可以使用 removeAttr()

$("#text").removeAttr('readonly');