简体   繁体   English

如果输入之一已填写,请禁用表行中的输入

[英]Disable input in table row if one of input filled

I have javascript code to disable other inputs if one is filled . 如果有一个输入,我有JavaScript代码来禁用其他输入。 I need it in table that comes out of database. 我需要它来自数据库。 The sad thing is that it only works with first table row and disable all inputs in table (but if input filled is not first nothing happens) 可悲的是,它仅适用于第一个表行并禁用表中的所有输入(但如果输入不是首先填充,则什么也不会发生)

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);

                    }
    });
});

My td from html table: 我从HTML表中得到的td:

<td><input type="text" class="inputA" value=""></td>
<td><input type="text" class="inputB" value=""></td>
<td><input type="text" class="inputC" value=""></td>

How to make it work for each line separated? 如何使其适用于每行分隔?

Use the same class on each input , create event on those input after that check the value of the input if she's not empty disable all of others input for this works in a line just select all input of the parent . 在每个input上使用相同的class ,在这些input上创建事件,然后如果她不为空,则检查inputvalue 。如果所有其他inputdisable ,则在一行中disableinput ,只需选择parent所有输入即可。

Try to avoid multiple test as you did. 尽量避免重复测试。 Not lisible and maintainable. 不易维护。

Example

 $(".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); } }); 
 .input:disabled{ background-color:LightBlue; border:solid 1px blue; } 
 <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> 

This solves your issue! 这样可以解决您的问题! Disable all input fields that doesn't have same class as the one being currently edited. 禁用所有与当前正在编辑的输入字段没有相同类的输入字段。

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

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);
        }
    });
});

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

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

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