dongou2019 2016-01-14 19: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-14 20: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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部