【发布时间】: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