1

親愛なるstackoverflowers、

特定のフィールドの ID と値の取得について質問があります。現在、次のコードがあります。

<div class="category_edit_form">
    <input type = "text" name = "category_edit_text" class = "category_edit_text" id = "'.$category->category_id.'">
    <input type = "button" class="category_edit_button" value = "Wijzig">
</div>

これは foreach ループの出力です。したがって、表示される各カテゴリには、名前を変更するためのテキストフィールドとボタンがあります。これは私のjQueryです:

jQuery(".category_edit_button").click( function() {
    jQuery.ajax({
        async: false,
        url: nieuwsbrief.updatecategory,
        type: 'POST',
        data: { category_id: jQuery('.category_edit_text').prop('id'), category_name: jQuery('.category_edit_text').val()},
        success: function(data) {
            alert(data);
        }
    });
}); 

警告するデータは次のとおりです。

echo $_POST['category_id']."en de naam is: ".$_POST['category_name'];

しかし、ボタンをクリックするたびに、最初のフィールドの値と id しか表示されません。したがって、2 番目のテキスト フィールドのボタンをクリックすると、2 番目のテキスト フィールドの ID と値の代わりに、最初のフィールドの ID と値がポップアップ表示されます。

どうすればこの問題を解決できますか?

4

3 に答える 3

1

使用する:

jQuery(".category_edit_button").click( function() {

    jQuery.ajax({
            async: false,
            url: nieuwsbrief.updatecategory,
            type: 'POST',
            data: { category_id: jQuery(this).parent().find('.category_edit_text').prop('id'), category_name: jQuery('.category_edit_text').val()},
                success: function(data) {
                        alert(data);
                }
    });

}); 
于 2013-06-04T11:07:56.737 に答える
0

取得するセレクターのスコープを設定する必要があります.edit_category_text

jQuery(".category_edit_button").click( function() {
    var $parent = $(this).parent();
    jQuery.ajax({
            async: false,
            url: nieuwsbrief.updatecategory,
            type: 'POST',
            data: { category_id: jQuery('.category_edit_text',$parent).prop('id'), category_name: jQuery('.category_edit_text',$parent).val()},
                success: function(data) {
                        alert(data);
                }
    });

}); 
于 2013-06-04T11:07:56.923 に答える
0

その代わりに使用します:

jQuery('.category_edit_text').prop('id')

このコード:

jQuery(this).prev('.category_edit_text').prop('id')

またはprevAll()直接前の要素でない場合

于 2013-06-04T11:08:22.957 に答える