我有一个ajax搜索功能,可以从数据库中生成一个名称列表。每个名称都作为表单按钮回显,因此当用户单击该名称时,另一个ajax调用将显示与该名称相关的所有信息。但是,它不起作用。我已经尝试了下面的ajax函数的几个变体,但要么根本没有发生,或者页面只是刷新而没有结果。 关于如何使其发挥作用的任何想法?
这是最新的ajax(什么都不做)
$(function GetInfo() {
$('form').on('click', function (e) {
var tourName = $('#tourName').val();
var FirstName = $('#FirstName').val();
var LastName = $('#LastName').val();
alert("PLEASE ENTER A NAME" + FirstName + LastName);
$.ajax({
type: "POST",
url: 'process.php',
data: "tourName=" + tourName + "&firstname=" + firstname + "&lastname=" + lastname,
success: function(data){
$("#search_results").html(data);
}
});
e.preventDefault();
});
});
这是生成表单(名称)的php循环:
$string = '';
if (mysql_num_rows($query)){
while($row = mysql_fetch_assoc($query)){
$FirstName = $row['FirstName'];
$LastName = $row['LastName'];
$Name = $row['FirstName']." ".$row['LastName'];
$string .= "<form method='post' action=''>
<input type='hidden' name='FirstName' value='$FirstName'>
<input type='hidden' name='LastName' value='$LastName'>
<input type='button' class='button' name='person_name' value='$Name' onClick='GetInfo()'></form><br /><br />\n";
}
}else{
$string = "No matches found!";
}
mysql_close($con);
echo $string;
只是因为任何人都有同样的问题,我得到以下代码:
function GetInfo(form) {
var person_name = form.person_name.value;
var tourName = form.tourName.value;
var firstname = form.FName.value;
var lastname = form.LName.value;
$.ajax({
type: "POST",
url: "process.php",
data: "person_name=" + person_name + "&tourName=" + tourName + "&firstname=" + firstname + "&lastname=" + lastname,
success: function(data){
$("#search_results").html(data);
}
});
return false;
}
以表格
$string .= "<form method='post' id='$form'>
<input type='hidden' name='tourName' value='$tourneyName'>
<input type='hidden' name='FName' value='$FirstName'>
<input type='hidden' name='LName' value='$LastName'>
<input type='button' class='button' name='person_name' value='$Name' onClick='GetInfo (this.form)'></form><br /><br />\n";
}
}else{
$string = "No matches found!";
}
答案 0 :(得分:0)
使用此:
$(function GetInfo(el) {
var tourName = $('#tourName').val();
var FirstName = $(el).siblings('[name=FirstName]').val();
var LastName = $(el).siblings('[name=LasstName]').val();
alert("PLEASE ENTER A NAME" + FirstName + LastName);
$.ajax({
type: "POST",
url: 'process.php',
data: { tourName: tourName, firstname: FirstName, lastname: LastName },
success: function(data){
$("#search_results").html(data);
}
});
e.preventDefault();
});
您需要更改要使用的HTML:
onclick='GetInfo(this)'
答案 1 :(得分:0)
您应该为表格和目标添加ID,而不是使用$('form')使用$('#yourformid')
答案 2 :(得分:0)
解决方案已在上面发布。现在看来已经很简单了。
答案 3 :(得分:0)
$('#send_email').click(function() {
$.ajax({
type : 'POST',
url : '<?php echo base_url()?>contact',
data : $( '#contact_form' ).serialize(),
success : function(msg){
$('#results').html(msg);
if(msg == "Successfully Subscribed"){
$( "#email_news" ).val('');
}
}
});
return false;
});