首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果其中一个输入已填充,则禁用表行中的输入。

如果其中一个输入已填充,则禁用表行中的输入。
EN

Stack Overflow用户
提问于 2016-10-14 07:18:40
回答 3查看 2.5K关注 0票数 1

我有javascript代码来禁用其他输入,如果一个被填充。我要把它放在数据库里的桌子上。可悲的是,它只适用于第一表行,并禁用表中的所有输入(但如果输入填充不是优先,什么都不会发生)。

Javascript:

代码语言:javascript
复制
  $(function(){
    $("input").on("keyup", function(){
            if($(this).hasClass("inputA") && $(".inputA").val()){
               $("input.inputB").prop("disabled", true);
               $("input.inputA").prop("disabled", false);
               $("input.inputC").prop("disabled", true);

            } else if($(this).hasClass("inputB") && $(".inputB").val()){
               $("input.inputA").prop("disabled", true);
                $("input.inputB").prop("disabled", false);
                 $("input.inputC").prop("disabled", true);

            } else if($(this).hasClass("inputC") && $(".inputC").val()){
               $("input.inputA").prop("disabled", true);
                $("input.inputB").prop("disabled", true);
                 $("input.inputC").prop("disabled", false);

            }  else {
                       $(".inputA, .inputB").prop("disabled", false);

                    }
    });
});

我在html表中的td:

代码语言:javascript
复制
<td><input type="text" class="inputA" value=""></td>
<td><input type="text" class="inputB" value=""></td>
<td><input type="text" class="inputC" value=""></td>

如何使每一行分开工作?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-10-14 07:26:11

在每个class上使用相同的input,在这些input上创建事件之后,检查输入的value,如果她不是空的,那么所有其他的input都在一行中工作,只需选择parent的所有输入。

尽量避免多次测试,就像你做的那样。不可移动和可维护。

示例

代码语言:javascript
复制
$(".input").on("keypress change keyup",function(){
   if($(this).val() != "")
   {
     $(this).parent().parent().find(".input").not($(this)).prop("disabled",true);
   }
   else
   {
     $(this).parent().parent().find(".input").prop("disabled",false);
   }
});
代码语言:javascript
复制
.input:disabled{
  background-color:LightBlue;
  border:solid 1px blue;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tr>
    <td>
      <input type="text" class="input" value="">
    </td>
    <td>
      <input type="text" class="input" value="">
    </td>
    <td>
      <input type="text" class="input" value="">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" class="input" value="">
    </td>
    <td>
      <input type="text" class="input" value="">
    </td>
    <td>
      <input type="text" class="input" value="">
    </td>
  </tr>
  </table>

票数 1
EN

Stack Overflow用户

发布于 2016-10-14 07:44:27

这解决了你的问题!禁用与当前编辑的输入字段没有相同类的所有输入字段。

演示:https://jsfiddle.net/3tf8ou3y/1/

代码语言:javascript
复制
jQuery(document).ready(function($) {
    $("tr").on("keyup", "input", function(event) {
        // get the current row
        var $row = $(event.delegateTarget);
        // get the class of the input field being edited
        var this_class = $(this).attr('class');

        if ($(this).val().length > 0) {
            // if the input field has a value, disable all
            // other input fields in the sam row
            $('input:not(.'+this_class+')', $row).prop('disabled', true);
        } else {
            // else enable all input fields
            $('input', $row).prop('disabled', false);
        }
    });
});
票数 1
EN

Stack Overflow用户

发布于 2016-10-14 07:32:50

您可以使用'jQuery(this).val()‘代替’jQuery(“.inputA”).val()或‘jQuery(“.inputB”).val()或jQuery(".inputC").val()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40037197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档