Javascript 如何通过ajax发送多个变量?
下面的代码将item的id发送到more.php以加载更多内容。它很好用。现在,为了添加更多的功能,我需要通过相同的代码向more.php发送更多的idJavascript 如何通过ajax发送多个变量?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,下面的代码将item的id发送到more.php以加载更多内容。它很好用。现在,为了添加更多的功能,我需要通过相同的代码向more.php发送更多的id <script type="text/javascript"> $(document).ready(function(){ $(document).on('click','.show_more',function(){ var ID = $(this).attr('id'); $('.show_more').hide(); $('.
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','.show_more',function(){
var ID = $(this).attr('id');
$('.show_more').hide();
$('.loding').show();
$.ajax({
type:'POST',
url:'more.php',
data:'id='+ID,
success:function(html){
$('#show_more_main'+ID).remove();
$('.display').append(html);
}
});
});
});
</script>
$(文档).ready(函数(){
$(文档)。在('单击','上。显示更多',函数(){
var ID=$(this.attr('ID');
$('.show_more').hide();
$('.loding').show();
$.ajax({
类型:'POST',
url:'more.php',
数据:'id='+id,
成功:函数(html){
$('#show_more_main'+ID).remove();
$('.display').append(html);
}
});
});
});
假设第二个id是
var catid=''代码>如何通过相同的ajax代码发送此catid
<代码>数据:{id:id,catid:catid}
是我应该做的事情,但在当前情况下,当我的数据携带数据:'id='+id,
时,我无法得到如何处理 你的头发应该是这样的。将数据指定为对象:
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click', '.show_more', function () {
var ID = $(this).attr('id');
$('.show_more').hide();
$('.loding').show();
$.ajax({
type: 'POST',
url: 'more.php',
data: { id:ID, userid: userid },
success: function (html) {
$('#show_more_main' + ID).remove();
$('.display').append(html);
}
});
});
});
$(文档).ready(函数(){
$(文档)。在('单击','上。显示更多',函数(){
var ID=$(this.attr('ID');
$('.show_more').hide();
$('.loding').show();
$.ajax({
键入:“POST”,
url:'more.php',
数据:{id:id,userid:userid},
成功:函数(html){
$('#show_more_main'+ID).remove();
$('.display').append(html);
}
});
});
});
要检索多个输入,我建议将它们组合成一个表单:
首先,使用提交按钮将输入嵌入表单中,您也不应该使用相同的名称两次,因为它现在不起作用,请创建唯一的名称
<form action="GET" id="myForm">
<input id="string" type="text" name="string" />
<input id="string2" type="text" name="string" />
<input type="submit" value="Go" />
</form>
这应该行得通。将其用作
{id:id}
用户数据:{id:id,userid:userid}
$('#myForm').submit(function(event) {
// Stop form from submitting normally
event.preventDefault();
var $form = $(this);
$.ajax({
type: "GET",
url: "retrieve.php",
data: $form.serialize(), //make it JSON
success: function() {
console.log("it worked");
}
});
});