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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效