dynamically building an array using jquery

时间:2017-12-18 07:40:32

标签: jquery asp.net

I have 4 checkbox in html. I want if any checkbox is checked its value is stored in array. I created a function but its shows the empty array

Subset(nameofthedataframe,Select = c(Namelist))
var arry = [];

function CheckBox(check) {
  debugger

  for (i = 0; i < check.length; i++) {
    if (check[i].checked == true) {
      arry.push(check[i].value);
      alert(arry);
    }
  }

}

$(document).ready(function() {

  $("#btn").click(function() {
    debugger;
    alert(arry);
  });
});

3 个答案:

答案 0 :(得分:1)

  1. You can use String sql="delete from buku where kode_buku=?"; int viewIndex = tabelDaftarBuku.getSelectedRow(); if(viewIndex != -1) { int modelIndex = tabelDaftarBuku.convertRowIndexToModel(viewIndex); DefaultTableModel model2 = (DefaultTableModel)tabelDaftarBuku.getModel(); model2.removeRow(modelIndex); } try { PreparedStatement pst=koneksi.prepareStatement(sql); pst.setString(1, jTKode1.getText()); pst.execute(); JOptionPane.showMessageDialog(null, "Deleted"); } catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } to loop the checkbox
  2. Use .each() to check check state

.is(":checked")
$(document).ready(function() {

  $("#btn").click(function() {
    var arry = [];
    $.each($(".chk"), function() {
      if ($(this).is(":checked")) {
        arry.push($(this).val());
      }

    })
    console.log(arry)
  });
});

答案 1 :(得分:0)

Several issues. The worst was to not get the checkboxes in the function.

You passed one box, not an array of boxes

plt.matshow(set_color_matrix, interpolation='nearest',cmap=cmap)
plt.title("Crystal Growth by DLA", y=1.08)
plt.xlabel("x", fontdict=dict(weight='bold'))
plt.ylabel("y", fontdict=dict(weight='bold'), rotation=0)
plt.savefig("Gifs_and_Images/{}_walkers.png".format(label), dpi=200)
var arry = [];  
function CheckBox(check) {
  arry = [];  
  $("."+check.className).each(function() {  // get the other checks
    if (this.checked) { // or jQuery: $(this).is(":checked");
      arry.push(this.value); // or jQuery $(this).val()
      alert(arry);
    }
  })
}

$(document).ready(function() {

  $("#btn").click(function() {
    alert(arry);
  });
});

答案 2 :(得分:0)

在JavaScript / Jquery代码中添加以下更改

$("#btn").click(function() {
    var arry = [];
    $.each($(".chk"), function() {
      if ($(this).is(":checked")) {
        arry.push($(this).val());
      }

    })
    alert(arry)
  });
});