简体   繁体   English

获取JavaScript中GridView中选中复选框的ID

[英]get id of checked checkbox in gridview in javascript

I am having checkbox in each itemTemplate of asp:gridview 我在asp:gridview的每个itemTemplate中都有复选框

I want to get ids or values of those many selected checkboxes using only javascript 我只想使用javascript来获取许多选定复选框的ID或值

In pure javascript I'm not sure about platform portability: you'd REALLY want jQuery or some other helper library here. 在纯JavaScript中,我不确定平台的可移植性:您真的想在这里使用jQuery或其他帮助程序库。

With jQuery: 使用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. 将为您提供所有选中复选框的ID和值。

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. 然后,您需要从resultsArray中提取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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