我有一個表單的頁面,我需要抓取某個div中A鏈接的所有ID屬性,並在提交之前將這些鏈接附加到表單的隱藏表單字段中。我該怎麼做?jQuery抓取表單提交之前
即
<form action="/myaction" id="myForm">
<div id="recipients">
<a id="1">recipient one</a>
<a id="2">recipient two</a>
<a id="3">recipient three</a>
</div>
<a href="javascript:void(0)" id="sendMail">Send</a>
</form>
<script>
//Capture form before sending
$("#sendMail").click(function() {
//Do something here to loop over the a tags in the recipients div,
//and add as a list to a new hidden form field
$('#myForm').trigger('submit');
});
</script>
任何想法?
順便說一句:ID **不能**以數字開頭,而是以字母開頭。 [見W3](http://www.w3.org/TR/html4/types.html#type-name) – Thorsten
@Thorsten - 除非您使用HTML5'<!DOCTYPE html>'doctype –
@James jQuery有問題在選擇器中使用以數字開頭的ID - 至少它達到了1.7,不知道現在HTML5是否允許這樣做。 –