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

來源:互聯網
上載者:User

複製代碼 代碼如下:
<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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.