【问题标题】:passing multiple id into one ajax function将多个 id 传递给一个 ajax 函数
【发布时间】:2020-09-24 16:24:01
【问题描述】:

我想在多个 id 上运行相同的 js ajax 函数,所以我可以从我的数据库中返回特定信息。在这里它确实运行了“id =“test”,但它会将它们全部返回。我如何让它们返回到它自己的

“身份证”

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);
                                    }
                                 });
                              });

【问题讨论】:

  • 绝不是 jquery 专家,但id^='test' 不是表示所有 id 都以 test 开头吗?
  • 您是否通过 console.log x 检查您实际发送的内容?
  • 是的,这确实意味着所有 id 都以 test 开头。
  • 我确实检查了 console.log。它确实通过了我想要的 1,2,3

标签: php html jquery laravel jquery-ui


【解决方案1】:

使用 $(this).html() 否则,对于每个 ajax 调用,$(" p[id^='test']").html(data.msg) 将被调用,即使是最后一个 ajax 调用。所以最后一个电话有 3 作为答案。所以它更新了前两个 ajax 调用。

$(".postbutton").each(function () {
                        var context = this
                        x = $(context).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)
                                    {
                                        $(context).html(data.msg);
                                    }
                                 });
                              });

【讨论】:

  • $(this) 与您的 ajax 函数中的$('.potbutton') 不相同!您可以设置 ajax 函数的 context 或将其保存到另一个变量中,该变量对您的 ajax 回调范围可见。这里有详细的answer
  • 太棒了! @zul 不要忘记将答案标记为已解决!
【解决方案2】:

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

在成功函数中也匹配你将返回所有可能的匹配 - 在本例中为 3

如果你使用已经建立的 ID 来匹配成功函数中的元素,比如

$(`#test_${x}`).html(data.msg);

【讨论】:

  • .prop 和 attr() 都完成了这项工作。但是你的 $(#test_${x}).html(data.msg);确实有点让我知道我应该在哪里改变,谢谢!
  • 它们不一样,只是在你的情况下它们可能是一样的,但最好使用合适的:)
【解决方案3】:

首先将您的所有 id 存储在数组中。 在你的ajax函数中传递这个数组之后。 像这样:-

           var id_list=[];
            $(".postbutton").each(function () {
                        x = $(this).prop('id').replace(/[^\d.]/g, '') // converting it to a set of ID to be passed to my controller
                        id_list.push(x);
             });
            $.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: id_list},
                dataType: 'JSON',
                /* remind that 'data' is the response of the AjaxController */
                success: function (data)
                    {
                        $(" p[id^='test']").html(data.msg);
                    }
                 });

【讨论】:

  • 但是如何对数组进行排序并将其传递回视图?
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 2011-06-04
  • 2019-04-21
  • 2013-09-27
  • 2021-10-05
  • 2023-01-11
  • 1970-01-01
  • 2012-08-02
相关资源
最近更新 更多