简体   繁体   English

如何在不影响其他行的情况下启用/禁用javascript中表行中的输入字段

[英]How to enable/disable input field in a table row in javascript without affecting other row(s)

I have a form with a dropdown menu followed by a text input field. 我有一个带有下拉菜单的表单,其后是一个文本输入字段。 The input box is disabled by default. 默认情况下,输入框是禁用的。 Selecting a specified option ("OTHER") from the menu should enable the text field. 从菜单中选择指定的选项(“ OTHER”)应启用文本字段。

Note: The user can add multiple row(s) if he clicks on the icon(+). 注意:如果用户单击图标(+),则可以添加多行。 The other row(s) will not be affected when a certain row selects "OTHER". 当某一行选择“其他”时,其他行将不受影响。 Only the row(s) having the option "OTHER" will be enable. 仅具有选项“ OTHER”的行将被启用。

<tr id="dataRow">
    <td>+</td>
    <td>-</td>                                           
    <td>
        <select onChange="checkOption(this);">
         <option value="A">Option A</option>
         <option value="B">Option B</option>
         <option value="C">Option C</option>
         <option value="OTHER">Other</option>  
        </select>
    </td>                            
    <td><input id="inputTextBox"></td>  
</tr>

JAVASCRIPT: JAVASCRIPT:

function checkOption(obj) {
        var input = document.getElementById("inputTextBox");
        input.disabled = obj.value != "OTHER";
}

Sample: 样品:

First Row: Selects OTHER (inputTextBox is enabled)
User add another Row: Selects Option A (inputTextBox is disabled)
User add another Row: Selects OTHER (inputTextBox is enabled)

The HTML is something like this when multiple row(s) is generated. 当生成多行时,HTML就是这样。 Sharing the same ids, options.. 共享相同的ID,选项。

<tr id="dataRow">
        <td>+</td>
        <td>-</td>                                           
        <td>
            <select onChange="checkOption(this);">
             <option value="A">Option A</option>
             <option value="B">Option B</option>
             <option value="C">Option C</option>
             <option value="OTHER">Other</option>  
            </select>
        </td>                            
        <td><input id="inputTextBox"></td>  
    </tr>
<tr id="dataRow">
        <td>+</td>
        <td>-</td>                                           
        <td>
            <select onChange="checkOption(this);">
             <option value="A">Option A</option>
             <option value="B">Option B</option>
             <option value="C">Option C</option>
             <option value="OTHER">Other</option>  
            </select>
        </td>                            
        <td><input id="inputTextBox"></td>  
    </tr>
<tr id="dataRow">
        <td>+</td>
        <td>-</td>                                           
        <td>
            <select onChange="checkOption(this);">
             <option value="A">Option A</option>
             <option value="B">Option B</option>
             <option value="C">Option C</option>
             <option value="OTHER">Other</option>  
            </select>
        </td>                            
        <td><input id="inputTextBox"></td>  
    </tr>

If you want to check the selected value and disable the input if the client selects other this will work. 如果您要检查选定的值并在客户端选择other值时禁用输入,则可以使用。

 function checkOption(obj) { var input = document.getElementById("inputTextBox"); if(obj.value=='OTHER'){ input.disabled=true; }else{ input.disabled=false; } } 
 <select onChange="checkOption(this);"> <option value="A">Option A</option> <option value="B">Option B</option> <option value="C">Option C</option> <option value="OTHER">Other</option> </select> <br> <input id="inputTextBox"> 


Or 要么


a new option with the value of other is created and appears at the top of the select. 将创建一个带有other值的新选项,并显示在所选内容的顶部。 You can give your select an ID and call this function and it will check the selected option after the dynamic option has been added. 您可以给选择一个ID并调用此函数,添加动态选项后它将检查所选的选项。

function checkOption() {
    var input = document.getElementById("inputTextBox");
    var Index = document.getElementById("Options").selectedIndex;
    if(document.getElementById("Options")[Index].value=='OTHER'){
        input.disabled=true;
    }else{
        input.disabled=false;
    }
}

<select id="Options" onChange="checkOption();">

Using selectedIndex 使用selectedIndex

document.getElementById("Options")[0] => A - Option A document.getElementById(“ Options”)[0] => A-选项A

document.getElementById("Options")[1] => B - Option B document.getElementById(“ Options”)[1] => B-选项B

document.getElementById("Options")[2] => C - Option C document.getElementById(“ Options”)[2] => C-选项C

document.getElementById("Options")[3] => OTHER - Other document.getElementById(“ Options”)[3] =>其他-其他

If you have any questions please leave a comment below and I will get back to you as soon as possible. 如果您有任何疑问,请在下面发表评论,我们会尽快与您联系。

I hope this helps. 我希望这有帮助。 Happy coding! 编码愉快!

Is jQuery an option? jQuery是一个选择吗?

If yes use on() 如果是,请使用on()

According to docs it binds event even to not yet existing elements. 根据文档,它甚至将事件绑定到尚不存在的元素。

I am not sure I understand what exactly you are trying to achieve but if its just enabling/disabling the text field something like this in your checkOption function should do the job: 我不确定我到底了解您到底要实现什么,但是如果它只是启用/禁用了文本字段,那么在checkOption函数中应该执行以下操作:

function checkOption(obj) {
  if (obj.value == "OTHER"){
    if( document.getElementById("inputTextBox") != null)
      {
        document.getElementById("inputTextBox").disabled = false;
      } 
  }
  if (obj.value != "OTHER")
  {
    if( document.getElementById("inputTextBox") != null)
      {
        document.getElementById("inputTextBox").disabled = true;
      } 
  }

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

相关问题 如何在启用/禁用按钮的帮助下禁用/启用表中的整个行? - How to disable/enable the entire row in table with help of enable/disable buttons? 如何在 jquery 中启用或禁用对表格行的单击? - How to enable or disable a click on a table row in jquery? Javascript根据选择其他字段禁用/启用输入字段 - Javascript disable/enable input field based on selection other field 如何删除选定的现有数据并添加新行而不影响Jquery数据表中的其他行 - How to remove selected existing data and add new row without affecting other rows in Jquery data table 无法启用/禁用html表行中的输入控件 - unable to enable/disable the input control in html table row 如何禁用同一行的其他输入字段 - How to disable other input fields of same row Javascript生成的表 - 当该行中的数字输入值发生变化时,如何更新该行中的其他单元格 - Javascript generated table - How to update other cells in a row when a number input value in this row changes 如何根据复选框禁用和启用表格行? - How to disable and enable table row based on check box? 如何启用/禁用表行内容-Angular.js - How can I Enable/Disable table row contents - Angularjs 如何禁用多个输入中的require并启用javascript中的其他输入 - How to disable require in multiple inputs and enable the other input in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM