简体   繁体   English

勾选复选框后如何自动禁用文本字段

[英]How to automatically disable a text field after checkbox is ticked

I am currently working on a project, that requires me to implement the following:我目前正在开展一个项目,该项目要求我实现以下内容:

I've got 2 input fields - a regular text field , and a checkbox .我有 2 个输入字段 - 一个常规文本字段和一个复选框

What I want to do is - if the user fills in the text field, the checkbox should be automatically disabled.我想要做的是 - 如果用户填写文本字段,复选框应自动禁用。 If the checkbox is checked instead, the text field should be disabled.如果选中复选框,则应禁用文本字段。

What I've come up with so far is:到目前为止我想出的是:

<input type="text" name="num-input1" id="dis_rm"  value="" 
onblur="document.getElementById('dis_per').disabled = (''!=this.value);" >
<input type="checkbox" name="num-input2" id="dis_per" value="" 
onblur="document.getElementById('dis_rm').disabled = (''!=this.value);" >

If I fill in the text field, the checkbox is successfully disabled.如果我填写文本字段,复选框将被成功禁用。 However, if I tick the checkbox, the text field remains available.但是,如果我勾选复选框,文本字段仍然可用。

What am I missing?我错过了什么?

If I were you I would:如果我是你我会:

Here is a little snippet to get you going:这是一个小片段,可以帮助您前进:

 const textInput = document.getElementById('element-ID-here'), checkbox = document.getElementById('element-ID-here'); function onTextInputBlurHandler(event) { // if textinput value is empty then // set disabled for checkbox to false // else // set disabled for chexbox to true } textInput.addEventListenet('blur', onTextInputBlurHandler);
 <input type="text" name="num-input1" id="dis_rm" value=""/> <input type="checkbox" name="num-input2" id="dis_per" value=""/>

With this info you should be able to get (a little) further.有了这些信息,您应该能够(一点)进一步了解。 When you do, update your question with your JavaScript code and I am sure people will be happy to help you further.当你这样做时,用你的 JavaScript 代码更新你的问题,我相信人们会很乐意为你提供进一步的帮助。

People are bringing up great suggestions in the comments and answers for better code design and quality, but from a purely functional point of view, there are two core things that you should do to get the functionality that you are describing:人们在评论和答案中提出了更好的代码设计和质量的好建议,但从纯粹的功能角度来看,您应该做两件核心事情来获得您所描述的功能:

1) As mentioned by Paul S. use the checked property for your checkbox logic. 1) 正如Paul S.所提到的,将checked属性用于您的复选框逻辑。 Right now, you are checking to see if the checkbox value is not an empty string, but it will always be an empty string, because that's the value that you've assigned to the element:现在,您正在检查复选框值是否不是空字符串,但它始终是空字符串,因为这是您分配给元素的值:

<input type="checkbox" name="num-input2" id="dis_per" value="" <----- *here*

Nothing else in your code is changing that, so it will always fail the logic check.您的代码中没有其他任何内容会改变它,因此它始终无法通过逻辑检查。

However, the checked property automatically switches between true and false as you check and uncheck the input.但是, checked属性会在您选中和取消选中输入时自动在truefalse之间切换。 To do the logic check that you are looking for using that, do this for your JavaScript!要执行您正在寻找的逻辑检查,请为您的 JavaScript 执行此操作!

document.getElementById('dis_rm').disabled = this.checked;

2) Switch the event that you are binding for (at least) the checkbox to the "change" event instead of "blur". 2)将您绑定(至少)复选框的事件切换到“更改”事件而不是“模糊”。 For checkboxes, the "change" event will trigger when you click on the checkbox (or hit space bar), but the element still maintains its focus.对于复选框,当您单击复选框(或点击空格键)时将触发“更改”事件,但元素仍保持其焦点。 The blur event Will only fire once the user moves the focus to another element of the page.模糊事件只会在用户将焦点移动到页面的另一个元素时触发。

I'd also recommend using "change" for the text field (there's no point in running the check, if the value is the same when you leave the field as it was when you entered it), but it's not as important since, from a timing point of view, when the "change" event fires, it happens immediately after the "blur" event, so, from the user's point-of-view, the behavior would be the same.我还建议对文本字段使用“更改”(如果您离开该字段时的值与输入时的值相同,则运行检查没有意义),但这并不重要,因为从从时间的角度来看,当“更改”事件触发时,它会在“模糊”事件之后立即发生,因此,从用户的角度来看,行为将是相同的。


When it's all said and done, if you made no other changes to your code to improve the code design/quality ( Thijs made some great suggestions, BTW), this is the minimum change that you would need to get the functionality that you want:总而言之,如果您没有对代码进行其他更改以提高代码设计/质量( Thijs提出了一些很好的建议,顺便说一句),这是获得所需功能所需的最小更改:

<input type="text" name="num-input1" id="dis_rm" value=""
       onblur="document.getElementById('dis_per').disabled = (''!=this.value);" >
<input type="checkbox" name="num-input2" id="dis_per" value=""
       onchange="document.getElementById('dis_rm').disabled = (this.checked);" >

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

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