当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JQuery一种取同级值的方式(比如你在GridView中)

JQuery一种取同级值的方式(比如你在GridView中)

2018年10月12日  | 移动技术网IT编程  | 我要评论

代码如下:

<:gridview id="gvreceipt" runat="server" width="100%" autogeneratecolumns="false" datakeynames="id" cssclass="grid" >
<columns>
<asp:templatefield>
<itemtemplate >
<input type="checkbox" id="chkreceipt" value='<%#eval("id") %>' name="chkreceipt" />
<input id="hdcustomercode" type="hidden" value='<%#eval("customercode") %>' />
<input id="hdcustomername" type="hidden" value='<%#eval("customer") %>' />
<input class="hdstatus" type="hidden" value='<%#eval("department") %>' />
</itemtemplate>
</asp:templatefield>
</asp:gridview>


你想取选中的checkbox后面隐藏域中的value,如下:

代码如下:


function selectreceipt()
{
var checknum = 0;
var customercode = "";
var type = "";
var url = "";
checknum = $("input:checked").length;
if (checknum > 1)
{
alert("只能选择一条记录进行收款!");
return false;
}
else
{
alert(checknum);
if (checknum == 1)
{
customercode = $("input:checked").next().attr("value"); //通过next()方法取,如果要取再下一个hdcustomername的值,可以.next().next()。
//customername = $("input:checked~#hdcustomername").val();//ie用id会报错,firefox不会
type = $("input:checked~.hdstatus").attr("value");//或者通过用class的方式取,
url = 'prereceiptdeposit.aspx?customercode=' + customercode + '&departmenttype=' + type;
}
else
{
url = 'prereceiptdeposit.aspx?customercode=' + '' + '&departmenttype=' + type;
}
alert(url);
universalopenwindowandbreak(640, 600, url, 1);
return true;
}
}


jquery--checkbox全选/取消全选

. 代码如下:


<html>
<head>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
</head>
<body>
<input type="checkbox" name="chk_list" id="chk_list_1" value="1" />1<br />
<input type="checkbox" name="chk_list" id="chk_list_2" value="2" />2<br />
<input type="checkbox" name="chk_list" id="chk_list_3" value="3" />3<br />
<input type="checkbox" name="chk_list" id="chk_list_4" value="4" />4<br />
<input type="checkbox" name="chk_all" id="chk_all" />全选/取消全选
<script type="text/javascript">
$("#chk_all").click(function(){
$("input[name='chk_list']").attr("checked",$(this).attr("checked"));
});
</script>
</body>
</html>


jquery.attr 获取/设置对象的属性值,如:
$("input[name='chk_list']").attr("checked"); //读取所有name为'chk_list'对象的状态(是否选中)
$("input[name='chk_list']").attr("checked",true); //设置所有name为'chk_list'对象的checked为true
再如:
$("#img_1").attr("src","test.jpg"); //设置id为img_1的<img>src的值为'test.jpg'
$("#img_1").attr("src"); //读取id为img_1的<img>src值
下面的代码是获取上面实例中选中的checkbox的value值:

. 代码如下:


<script type="text/javascript">
//获取到所有name为'chk_list'并选中的checkbox(集合)
var arrchk=$("input[name='chk_list]:checked");
//遍历得到每个checkbox的value值
for (var i=0;i<arrchk.length;i++)
{
alert(arrchk[i].value);
}
</script>


下面是用$.each()遍历的代码:

代码如下:


<script type="text/javascript">
var arrchk=$("input[name='chk_list']:checked");
$(arrchk).each(function(){
window.alert(this.value);
});
});
</script>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网