简体   繁体   English

如何通过在元素外部单击来将Element设置为ReadOnly

[英]How to set Element as ReadOnly by clicking outside the element

I have an <input> element set to readonly by default: 我有一个<input>元素默认设置为readonly

<input readonly id="uniqueId" type="text" ondblclick="onDoubleClick(this.id)">

If the user double-clicks this element it will be set to editable with: 如果用户双击该元素,它将通过以下方式设置为可编辑:

function onDoubleClick(id){
    var element = $('#'+id);
    if (element.prop('readonly')==true) { element.prop('readonly', false);}
    else { element.prop('readonly', true); } 

Apparently this <input> element will stay editable up until the user double clicks it again. 显然,此<input>元素将保持可编辑状态,直到用户再次双击它为止。 I wonder if I could link a mouse click that happens outside of the <input> element to turn it into readonly again. 我想知道是否可以链接发生在<input>元素外部的鼠标click ,以再次将其变为只读。 How to achieve it? 如何实现呢?

You can make any clicks on the document make the element readonly. 您可以在文档上进行任何单击以使元素变为只读。

 function onDoubleClick(id) { var element = $('#' + id); if (element.prop('readonly') == true) { element.prop('readonly', false); } else { element.prop('readonly', true); } } function inputClicked(e) { e.stopPropagation(); } $(document).click(function() { $('#uniqueId').prop('readonly', true); }); 
 input[readonly] { background: lightgray; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input readonly id="uniqueId" type="text" onclick="inputClicked(event)" ondblclick="onDoubleClick(this.id)"> 

Two approaches which I suggest 我建议两种方法

Method 1 - Using one 方法1-使用one

Add this inside your current function 将此添加到当前函数中

$('html').one('click',function() {
    $("#uniqueId").prop('readonly', true); 
  });

Explanation: when ever you double click and make the input editable The above script will attach one click event to HTML which makes the input read-only. 说明:当过您双击使输入编辑以上脚本将附上one click事件到HTML这使得输入只读。 This event works only once as we have used one . 此事件只能作为我们已经用过一次one

So technically when ever you make the input editable a new event is attached which will make input read-only on further click. 因此,从技术上讲,只要您使输入可编辑,就会附加一个新事件,该事件将使输入在进一步单击时变为只读状态。

Method 2 - Checking if the click was outside the element 方法2-检查点击是否在元素外部

Add this into your script. 将此添加到您的脚本。

$('html').on('click',function(e){
  if(!$(event.target).is('#uniqueId'))
  {
    $("#uniqueId").prop('readonly', true); 
  }   
});

Explanation: when ever there is a click on you HTML you will check if the click was on the input, If not then make it readonly. 说明:当您单击HTML时,您将检查单击是否在输入上;如果没有,则将其设置为只读。

One option is to add a blur event to the input box, so it will make field readonly even if user hits TAB and moves out of field. 一种选择是将blur事件添加到输入框,这样即使用户单击TAB并移出字段,该字段也将使字段变为只读。

 $("input").on("blur", function() { $(this).prop('readonly', true); }) function onDoubleClick(id) { var element = $('#' + id); if (element.prop('readonly') == true) { element.prop('readonly', false); } else { element.prop('readonly', true); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input readonly id="uniqueId" type="text" ondblclick="onDoubleClick(this.id)"> 

you could rely on the blur event to take action when it loses focus 您可以依靠blur事件在失去焦点时采取措施

 function onDoubleClick(elem) { console.log('toggling readonly property'); var element = $(elem); element.prop('readonly', !element.prop('readonly')); // toggle } function lostFocus(elem) { console.log('lostFocust'); var element = $(elem); element.prop('readonly', true); } 
 input { background-color: white; } input[readonly] { background-color: #ccc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input readonly id="uniqueId" type="text" ondblclick="onDoubleClick(this)" onblur="lostFocus(this)"> 

No jQuery needed here since we already have access to the element when using the ondblclick handler, and you can add a function to the onfocusout or onblur event handler of your element: 这里不需要jQuery,因为使用ondblclick处理程序时我们已经可以访问该元素,并且可以向该ondblclickonfocusoutonblur事件处理程序中添加一个函数:

<input readonly id="uniqueId" type="text" ondblclick="onDoubleClick(this)" onfocusout="onFocusOut(this)">

<script>
  function onDoubleClick(element){
    element.readOnly = !element.readOnly;
  }

  function onFocusOut(element) {
    element.readOnly = true;
  }
</script>

Same as the ondblclick attribute you can use the onblur attribute of the input element. ondblclick属性相同,您可以使用input元素的onblur属性。 It will run that function once that element gets blurred (out of focus). 一旦该元素变得模糊(失去焦点),它将运行该功能。

 function onDoubleClick(el) { var element = $(el); if (element.prop('readonly')==true) { element.prop('readonly', false); } else { element.prop('readonly', true); } } function onBlur(el) { $(el).prop('readonly', true); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input readonly id="uniqueId" type="text" ondblclick="onDoubleClick(this)" onblur="onBlur(this)"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM