Php 将多个id传递给一个ajax函数
我希望在multiples id上运行相同的js ajax函数,以便从数据库返回特定信息。在这里,它确实运行了“id=”test,但它将它们返回到它的所有部分。如何使它们返回到它自己的“id” htmlPhp 将多个id传递给一个ajax函数,php,html,jquery,laravel,jquery-ui,Php,Html,Jquery,Laravel,Jquery Ui,我希望在multiples id上运行相同的js ajax函数,以便从数据库返回特定信息。在这里,它确实运行了“id=”test,但它将它们返回到它的所有部分。如何使它们返回到它自己的“id” html 由于ID是在您的情况下不会更改的属性-请尝试将.prop替换为.attr(),以获取ID的值 另外,匹配函数将返回所有可能的匹配-在本例中为3 另外,如果您在成功函数中使用已建立的ID来匹配元素(如 $(`#test_${x}`).html(data.msg); 使用$(this.html()
由于ID是在您的情况下不会更改的属性-请尝试将
.prop
替换为.attr()
,以获取ID的值
另外,匹配函数将返回所有可能的匹配-在本例中为3
另外,如果您在成功函数中使用已建立的ID来匹配元素(如
$(`#test_${x}`).html(data.msg);
使用$(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);
}
});
});
首先将您的所有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);
}
});
当然不是jquery专家,但是
id^='test'
不是意味着所有id都是从test开始的吗?你有没有使用console.log x来检查你实际发送的内容?是的,它确实意味着所有id都是从test开始的。我检查了console.log。它确实通过了1,2,3,这是我想要的。prop和attr()都完成了任务。但是你的$(\test}{x}
).html(data.msg);确实在某种程度上给了我一个想法,我应该在哪里进行更改,谢谢!它们不一样,只是在您的情况下它们可能是一样的,但最好使用合适的:)但是我如何排序并将数组传递回视图?$(此)
与$('.potbutton')不同
在您的ajax函数中!您可以设置ajax函数的上下文
,也可以将其保存到另一个变量中,该变量对ajax回调的作用域可见。下面是一个非常详细的示例!@zul别忘了将答案标记为已解决!
$(".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);
}
});
});
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);
}
});