简体   繁体   中英

get id of checked checkbox in gridview in javascript

I am having checkbox in each itemTemplate of asp:gridview

I want to get ids or values of those many selected checkboxes using only javascript

In pure javascript I'm not sure about platform portability: you'd REALLY want jQuery or some other helper library here.

With jQuery:

var values = [];
var ids = [];
jQuery.each(jQuery("input:checkbox").find(":checked"), function(){
values.push(jQuery(this).val());
ids.push(jQuery(this).attr("id");
});

will give you the ids and values of all the checked checkboxes.

EDIT: ugly, but this might work...

var values = [];
var ids = [];
var inputs = document.getElementsByTagName("input");
var i;
for(i=0;i<inputs.length;i++){
  if(inputs[i].hasAttributes() && inputs.getAttribute('type') == "checkbox" && inputs.getAttribute('checked')){
     values.push(inputs[i].getAttribute('value'));
     ids.push(inputs[i].getAttribute('id'));
  }
}

Let me know if that does what you want.

I am not exactly sure on what you are trying to do but this might help you out. This will get all of the inputs on the screen and process only the checked ones.

var inputList = document.getElementsByTagName("input");
var resultsArray = [];

for(var i = 0; i < inputList.length; i++) {
    if (inputList[i].getAttribute("checked") == true) {
        resultsArray.push(inputList[i]);
    }
}

Sorry, forgot to tell you that this would be a list of elements. You will then need to extract them however you want to from resultsArray.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM