将动态创建的下拉列表的自定义属性值添加到另一个元素

Adding custom attribute values of dynamically created dropdowns to another element

本文关键字:添加 另一个 元素 自定义属性 动态 创建 下拉列表      更新时间:2023-09-26

我在这里有一点HTML:

<tr taskId="(#=obj.task.id#)" assigId="(#=obj.assig.id#)" class="assigEditRow" >
            <td><select name="resourceId" class="get-resources formElements"></select></td>
            <td><span class="resources-units"></span></td>
            <td><span class="resources-quantity"></span></td>
            <td><input type="text" placeholder="Required Q"></td>
            <td align="center"><span class="teamworkIcon delAssig" style="cursor: pointer">d</span></td>
</tr>

这里还有一点JS:

'use strict';
    function addResourceFunction(){
      let ResourcesJSON = (json) => {
        let Resources = json;
        console.log(Resources);
          let contactsLength = json.length;
          let arrayCounter = -1;
          let resID;
          let resName;
          let resUnit;
          let resQuantity;
          let Option = $('<option />');
          let assignedID = $('tr.assigEditRow:last').attr("assigId");
          while(arrayCounter <= contactsLength) {
            arrayCounter++;
            resID       = Resources[arrayCounter].ID;
            resName     = Resources[arrayCounter].name;
            resUnit     = Resources[arrayCounter].unit;
            resQuantity = Resources[arrayCounter].quantity;
            $('.assigEditRow').last().find('select').append($('<option>', {
              value: resName.toString(),
              text: resName.toString(),
              resourceID: resID.toString(),
              resourceUnit: resUnit.toString(),
              resourceQuantity: resQuantity.toString()
            }));
          }
      }
      $.getJSON("MY JSON URL IS HERE", function(json) {
        ResourcesJSON(json);
      });
    };

所以这里实际发生的事情是:我从URL(JSON数组)中获取数据,点击时触发addResourceFunction()来创建一个新的表行,并添加一个带有从数组传递的选项的新select。正如您从我的HTML标记中看到的,select输入被放置在td.get-resources中,所有这些都很好。我设置了日期,填充了选择字段,一切都很好。我可以添加任意多的行/选择下拉列表。

此外,每个选项都有一些自定义属性(你可以在上面的JS代码中看到),我想把这些属性的值添加到行的第二列和第三列(在HTML中,它们是span.resources-units和span.resources quantity)。问题是,我不知道如何使其1:1工作,这意味着一个选择下拉列表只"改变"自己行的单位和数量。以下是代码:

let idCounter = 1;
    $(document).on('change', '.get-resources', function() {
      $('.assigEditRow').last().find('.resources-units').attr('id', 'units-' + idCounter);
      $('.assigEditRow').last().find('.resources-quantity').attr('id', 'quantity-' + idCounter);
      this.resourceUn = $( ".get-resources option:selected" ).attr( "resourceUnit" );
      this.resourceQuant = $( ".get-resources option:selected" ).attr( "resourceQuantity" );
      $('#units-' + idCounter).append(this.resourceUn);
      $('#quantity-' + idCounter).append(this.resourceQuant);
      idCounter++;
    });

结果是,如果我添加一个选择输入,并更改选项,事情就会成功。当我添加另一个并更改其选项时,它会获得第一个的属性。添加更多相同的东西。无论我更改什么,它都会采用添加的第一个项目的属性值。

尝试从元素而不是从变量中获取id,因为您总是用计数器的id更新元素,而不是用单击的行的id来更新元素。

嗯,柜台到底是干什么的?我越看越不明白。我所知道的是,使用idCounter引用正确的行并没有选择正确的元素。

你想做一些类似的事情

$(document).on('change', '.get-resources', function() {
    //var row = this;
    this.find(/* Some path to the second column */).att(/* some att to change */);
    this.find(/* Some path to the third column */).att(/* some att to change */);
});

其中,您总是再次使用该行作为根,而不是查找某个id,因此只更新该行。

本机:

<table>
    <tr>
        <td>
            <select>
                <option data-text="resName1" data-resourceID="resID1" data-resourceUnit="resUnit1" data-resourceQuantity="resQuantity1">1</option>
                <option data-text="resName2" data-resourceID="resID2" data-resourceUnit="resUnit2" data-resourceQuantity="resQuantity2">2</option>
                <option data-text="resName3" data-resourceID="resID3" data-resourceUnit="resUnit3" data-resourceQuantity="resQuantity3">3</option>
            </select>
        </td>
        <td>
            <div class="column2"></div>
        </td>
        <td>
            <div class="column3"></div>
        </td>
    </tr>
</table>
<script>
document.addEventListener('change', function ( event ) {
    var select = event.target,
        option = select.options[select.selectedIndex],
        values = {
            'text' : option.getAttribute('data-text'),
            'resourceID' : option.getAttribute('data-resourceID'),
            'resourceUnit' : option.getAttribute('data-resourceUnit'),
            'resourceQuantity' : option.getAttribute('data-resourceQuantity')
        },
        row = select.parentNode.parentNode,/* depending on how deep the select is nested into the tr element */
        column2 = row.querySelector('.column2'),
        column3 = row.querySelector('.column3');
    column2.textContent = 'some string with the values you want';
    column3.textContent = 'some string with the other values you want';
});
</script>

基本上,您从更改的选择开始,从中可以获得单击的选项节点。然后,您可以从该选项中获得所需的属性。然后向上移动几个节点到父行,并找到该行中的两列。然后您可以设置这两列的内容。