将多个id传递给一个ajax函数

问题描述 投票:0回答:1

我想对倍数id运行相同的js ajax函数,因此我可以从数据库中返回特定信息。在这里,它确实通过“ id =“ test”运行,但将它们全部返回。如何使它们返回自己的位置

” id“

html

       <div>
           <p  class="postbutton" id="test_1" > </p> \\supposed to return 1, but it returns 3//
           <p  class="postbutton" id="test_2" > </p> \\supposed to return 2, but it returns 3//
           <p  class="postbutton" id="test_3" > </p> \\supposed to return 3, but it returns 3//
       </div>

我的脚本功能

$(".postbutton").each(function () {
                        x = $(this).prop('id').replace(/[^\d.]/g, '') // converting it to a set of ID to be passed to my controller

                            $.ajax({
                                /* the route pointing to the post function */
                                url: '/postajax',
                                type: 'POST',
                                /* send the csrf-token and the input to the controller, Laravel stuff */
                                data: {_token: CSRF_TOKEN, message: x},
                                dataType: 'JSON',
                                /* remind that 'data' is the response of the AjaxController */
                                success: function (data)
                                    {
                                        $(" p[id^='test']").html(data.msg);
                                    }
                                 });
                              });
php html jquery laravel jquery-ui
1个回答
0
投票

由于ID是属性,在您的情况下不会更改-尝试将.prop替换为.attr()以获取ID的值。

© www.soinside.com 2019 - 2024. All rights reserved.