简体   繁体   English

单击其他输入时禁用输入字段

[英]Disable input fields when clicking the other input

I want to disable selected input fields when the one of the chosen inputs is clicked. 单击所选输入之一时,我想禁用所选输入字段。

Example: 1. User selects input field "Name" and input field "Company" and "Company Number" is disabled; 示例:1.用户选择输入字段“名称”,输入字段“公司”和“公司编号”被禁用; 2. The field is disabled upon "entering" into field, not writting in it. 2.在“输入”到字段中而不是在其中写入时,该字段被禁用。

This was my code; 这是我的代码; it works, but not exactly as intended. 它有效,但不完全符合预期。 It disables when user starts typing. 用户开始输入时它将禁用。 I want to disable it on "enter"/on click. 我想在“输入” /单击时禁用它。

 var disableField = function () { var state = document.getElementById("name").value.length > 0; document.getElementById("company").disabled = state; document.getElementById("number").disabled = state; }; 

Any suggestions? 有什么建议么? Thank you in advance! 先感谢您!

You should use .onfocus event. 您应该使用.onfocus事件。

// name, company, company_number are the elements

function func(){
    if(this.value.length == 0) company_number.disabled = true;
    else company_number.disabled = false;
}

name.onfocus = func;
company.onfocus = func;

Try using the onfocus event like this: 尝试使用onfocus事件,如下所示:

<input type="text" class="form-control" id="name" placeholder="Name" onfocus="disableField()"/>

This event activates when you click and the focus is set in the input field. 当您单击并在输入字段中设置焦点时,将激活此事件。

The opposite event (when clicking out of the field) is onblur 相反的事件(当单击该字段时)为onblur

you tagged your question under jQuery then why not you tried some jQuery! 您在jQuery下标记了您的问题,然后为什么不尝试一些jQuery! use something like this- 使用这样的东西-

<script>
$('#elementId').click(function(){
  $("#someElementId").attr('disabled', true);
});
</script>

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

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