dongtan6543 2017-04-09 04:25
浏览 80
已采纳

使用Jquery显示不同输入的唯一ID /类增量

I've got a form which currently works as a single line allowing different input fields based on the Production drop down.

i.e. If you choose CPE or CPM you get the inputs for Impressions and Targeting. If you choose Eggs2Go then you only get Impressions.

While this works so far, if I add a line and use the drop down menu it affects the first ID tag changing that line and not the new lines options.

function showTargetCheck(that) {
        if (that.value == "CPM" || that.value == "CPE" || that.value == "SWIFT") {
            document.getElementById("showTarget").style.display = "block";
            document.getElementById("showImpressions").style.display = "none";
        }

        else if (that.value == "EGGS") {
            document.getElementById("showImpressions").style.display = "block";
            document.getElementById("showTarget").style.display = "none";
        }

        else {
            document.getElementById("showTarget").style.display = "none";
            document.getElementById("showImpressions").style.display = "none";
        }
    }

Is there a way I can give an auto increment to each new line so that the showTarget and ShowImpressions act individually?

Details in the fiddle

  • 写回答

3条回答 默认 最新

  • duandi5328 2017-04-09 04:54
    关注

    You can't repeat ID's in a page, they must be unique by definition.

    The common approach to repeating rows like this is to use common classes for like elements and isolate instances within your event handlers by looking up to parent row element and then looking inside that specific row for needed elements

    Assume we change all the repeating ID's to same class name you would then do something like the following in jQuery

    $('#tableContainer').on('change', 'select', function(e){
      // get row instance starting from `this` 
      var $row = $(this).closest('.tableRow'),
          currVal = $(this).val(),
          // flags for toggling various classes 
          showImpressions = false,
          showTarget = false;
    
       if (currVal  == "CPM" || currVal  == "CPE" || currVal  == "SWIFT") {
          showTarget = true;
       }
       // other conditionals to set flags based on other values
    
       // now toggle display of instances of classes within row instance   
       $row.find('.showTarget').toggle(showTarget);
       $row.find('.showImpressions').toggle(showImpressions);
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算