使输入文本不可编辑,然后使用jQuery在3秒内使其再次可编辑

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

本文关键字:编辑 3秒 然后 文本 输入 jQuery      更新时间:2023-09-26

我有一个输入文本,当你按回车键时,它应该使输入文本不可编辑,然后在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>

结尾有一个简单的拼写错误,有一个额外的括号,删除它

$(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');