首页 > 解决方案 > 点击 2 个 id 更新 .map() 数组 jquery

问题描述

如何在第二个按钮单击时更新 .map(maped) 数组。我需要存档的是第二个按钮单击更新数组。

我需要注意变化,然后再打电话.map()

$("#send, #more_column").click(function(e) {
  if (this.id == 'more_column') {
    console.log("Second button click"); //im inside second button click
    // here i need to update the device_id[] array add one more item to array or more
  }

  device_id = $('input:enabled[name="device_id[]"]').map(function() {
    return $(this).val(); // $(this).val()
  }).get();

});

单击第二个按钮时如何更新device_id数组。如果再次单击第二个按钮,则再次更新device_id数组。

有任何想法吗 ?

标签: javascriptjquery

解决方案


在这种情况下,混合事件处理程序没有意义。最好保持代码简单,所以有一个添加按钮的事件处理程序和一个发送按钮的事件处理程序,并使添加按钮也调用发送处理程序......

function send() {
    device_id = $('input:enabled[name="device_id[]"]').map(function() {
        return $(this).val(); // $(this).val()
    }).get();
}

function add() {
    // add whatever you need to add here
    // execute the "send" handler...
    send();
}

$("#send").on("click", send);
$("#more_column").on("click", add);

推荐阅读