开发者

Loop through checkboxes and gather names of the ones that are checked - JQuery

<input type="checkbox" name="SummaryInformation">Summary Information<br />
                        <input type="checkbox" name="ProductLegs">Product Legs<br />
                        <input type="checkbox" name="AmortizationOptions">Amortization Options<br />
                        <input type="checkbox" name="Values">Values<br />
                        <input type="checkbox" name="Rates">Rates<br />
                        <input type="checkbox" name="RatesSpecific">Rates (All-In-Rate, PV01)<br />
                        <input type="checkbox" name="AmortizationSchedule">Amortization Schedule<br />
                        <input type="checkbox" name="SponsorInfo">Sponsor/Affiliate Info<br />
                        <input type="checkbox" name="BorrowerInfo">Borrower Info<br />
                        <input type="checkbox" name="SponsorContacts">Sponsor/Affiliate Contacts<br />
                        <input type="checkbox" name="CashFlows">Cash Flows<br />
                        <input type="checkbox" name="PrePayment">Pre-Payment<br />
                        <input type="checkbox" name="FutureExposure">Potential Future Exposure<br />
                        <input type="checkbox" n开发者_开发问答ame="FutureExposureSpecific">Potential Future Exposure (Max Number and Date Only)<br />
                        <input type="checkbox" name="History">History<br />

I need to loop through all of these in JQuery and create an array of the names of the ones that are checked. How would I do this?


var names = [];
$("checkbox:checked").each(function(){
   names.push($(this).attr("name"));
})

you might want to add a div around the checkboxes you want this done to, so that you dont loop through ALL checkboxes on the screen.

if you did this the selector would change to:

$("#mydiv checkbox:checked")

where mydiv is the id of that div


var arr = [];

$('input[type=checkbox]').is(':checked').each(function(){
     arr.push($(this).attr('name'));
});

alert(arr.join('-'));


Use the checked selector:

$(':checked').each(function(){
  ...
});


It's as simple as

$("input:checked").each(...);

Here is a Live Demo: http://jsfiddle.net/vFmNh/3/


Use filter it's faster.

$('input').filter(function() {return this.checked})

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜