[英]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!
编码愉快!
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.