DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> JQuery一種取同級值的方式(比如你在GridView中)
JQuery一種取同級值的方式(比如你在GridView中)
編輯:JQuery特效代碼     
. 代碼如下:
<asp: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>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved