0

特定の div 内の A リンクのすべての ID 属性を取得し、送信前に非表示のフォーム フィールドにそれらをフォームに添付する必要があるフォームを含むページがあります。どうすればこれを行うことができますか?

すなわち

<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>

何か案は?

4

3 に答える 3

2

これらの行に沿って何かを始める必要があります。

$("#sendMail").click(function() {
    var recipients = "";
    $("#recipients a").each(function() {
        recpients += this.id + ",";
    });
    $("#someHiddenField").val(recipients);
    $("#myForm").trigger("submit");
});

を使用eachして要素のセットを反復処理し、値aを含む文字列を作成し、非表示の入力要素の値をその文字列に設定するために使用できます。上記の例では、コンマで区切られた文字列が取得され、末尾にコンマが付きます。あなたが探しているものを正確に知らないので、それを変更したいかもしれません。idval

于 2011-11-10T11:11:58.793 に答える
1

追加機能を使用して、新しい非表示フィールドを作成できます。

したがって、受信者のすべてのタグをループしaて、新しい非表示フィールドを追加するには、次のようにします。

$("#recipients a").each(function() {
    $("#myform").append('<input type="hidden" name="'+this.id+'" value="'+$(this).text()+'" />')
});

注:非表示フィールドの名前と値を指定しなかったthis.idため、名前には$(this).text()を使用し、値には$(this).text()を使用することを推測しました。

于 2011-11-10T11:12:39.263 に答える
0
try this

var Anchors = $("body").find("a")
var str = "";
    alert(Anchors.length);

    $(Anchors).each(function(){
        if(str !== ""){
            str+=",";   
        }

        str+=$(this).attr("id");
    })

 $("sometextfieldid").val(str);
于 2011-11-10T11:28:39.563 に答える