dongou2019 2016-01-15 03:06
浏览 39
已采纳

根据用户的选择,禁用在多行上动态生成的文本框

The system asks the user to identify the number of records they would like to enter, based on their selection (say 3), the system displays three text boxes.

    <?php for($i=1; $i<=$rows; $i++) { ?> // $i is 3 here
      <input type="text" name="company_name[]" id="cn[]"> 
      <input type="text" size="3"name="entertainment[]" id="en[]">
    <?php } ?>

So this will generate 3 rows, with the same text boxes. If the user enters a value in the 1st row for entertainment, the company name should get grayed out or vice versa. Similarly it should do the same for the 2nd and 3rd line as well - based on which text box they fill.

How do I do that via js/jquery?

P.S: I am not a good front-end developer, so I could use all the help I can get.

Thanks.

  • 写回答

1条回答 默认 最新

  • doucuo8618 2016-01-15 04:28
    关注

    First wrap then into <div>.
    Element Id should be unique, change it as following

    <?php for($i=1; $i<=$rows; $i++) { ?> // $i is 3 here
    <div>
    <input type="text" name="company_name[]" onchange="grayOther(this);" id="cn_<?php echo $i ?>"> 
    <input type="text" size="3"name="entertainment[]" onchange="grayOther(this);" id="en_<?php echo $i ?>">
    </div>
    <?php } ?>
    

    Then here is the js.

    <script>
    function grayOther(elem){
        elem = $(elem); // convert to jquery object
    
        if (elem.val().length == 0) {
            elem.siblings().prop('disabled',false);
        }else {
            if (elem.is("[name='company_name[]']")){
                elem.siblings("[name='entertainment[]']").prop('disabled',true);
            }else {
                elem.siblings("[name='company_name[]']").prop('disabled',true);
            }
        }
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器