通过jQuery迭代Gridview [英] Iterate Gridview through jquery
问题描述
假设我有一个gridview,并且我的gridview有很多行,包括页眉,页脚和寻呼机.
suppose i have a gridview and my gridview has many rows including header and footer and pager.
每行中都有一些文本框,下拉列表和复选框.
in every row there is some textbox, dropdown and checkbox.
就像两个用于雇员编号和雇员姓名的文本框,一个用于性别的下拉菜单,一个用于选择是否毕业的复选框.
like two textbox for employeeid and employeename,one dropdown for sex, one checkbox for selecting graduate or not.
所以我需要知道如何通过jquery迭代gridview,以及如何迭代除header,footer和pager之外的每一行.
so i need to know that how could i iterate gridview through jquery and also iterate each row excluding header,footer and pager.
所以我需要从每一行的文本框,下拉列表和复选框中提取值,并构建用于将数据发送到服务器的json字符串.
so i need to extract values from textbox,dropdown and checkbox from each row and build json string for sending data to server.
像构建json字符串
var json = "{'Name':'" + $("input[id*='txtName']").val() +
"','Subject':'" + $("input[id*='Subject']").val() +
"','Email':'" + $("input[id*='Email']").val() +
"','Message':'" + jQuery.trim($('.message').val()) + "'}";
谢谢
推荐答案
尝试一下.
像这样将RowStyle添加到GridView标记中
Add RowStyle to the GridView markup like this
<RowStyle CssClass="row" />
<AlternatingRowStyle CssClass="row" /><!-- If needed -->
现在GridViewRow
中的所有tr
都将具有<tr class="row">
.然后在按钮单击事件中调用此脚本.
Now all the tr
s in the GridViewRow
will have <tr class="row">
. Then call this script inside your button click event.
var items = [];
$(".row").each(function() {
var item = {};
item.ID = $.trim($(this).find("input[id*='ID']").val());
item.Name = $.trim($(this).find("input[id*='Name']").val());
item.Gender = $(this).find("select[id*='Gender']").val();
item.IsGraduate = $(this).find("input[id*='IsGraduate']").is(':checked');
items.push(item);
});
var DTO = JSON.stringify(items);
对于以下浏览器,请添加对 json2.js 的引用没有JSON支持
Please add reference to json2.js for browsers that do not have JSON support
模拟: http://jsfiddle.net/naveen/P8Aue/
这篇关于通过jQuery迭代Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!