我有这 4 个页面并且工作正常,但现在我需要将输入字段中键入的"单词"发送到另一个页面 (pag.php 2.php),同时它使用此 javascript 代码发送到 (pag1.php) 脚本 (script.php)。
索引.php
<form method="post">
<input type="text" name="word" id="word" onkeyup="getSugest(this.value);">
</form>
<div class='search'><img ..... searching.gif></div>
<div id="sugest">
<div id="resultIndex"></div>
<div id="buttonsIndex">
<ul>
<?
for($i=1; $i<=page; $i++;){
echo "<li id='".$i."'>".$i."</li>";
}
?>
<ul>
</div>
</div>
脚本.js
function getSugest(value){
if(value != ""){
if (callPage1(value) || callPage2(value)) {
var data1 = callPage1(value);
var data2 = callPage2(value);
//var combinedData = combine as data1 and data2 as you want
$("#sugest").html(combinedData);
} else {
$("#sugest").html("Theres nothing in DB!");
}
}
}
function callPage1(value) {
$.post("pag1.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
function callPage2(value) {
$.post("pag2.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
$(document).ready(function(){
function showLoader(){
$('.search').fadeIn(200);
}
function hideLoader(){
$('.search').fadeOut(200);
};
$("#buttonIndex li").click(function(){
showLoader();
$("#buttonIndex li").css({'background-color' : ''});
$(this).css({'background-color' : '#D8543A'});
$("#resultIndex").load("pag1.php?page=" + this.id, hideLoader);
return false;
});
$("#buttonCar li").click(function(){
showLoader();
$("#buttonCar li").css({'background-color' : ''});
$(this).css({'background-color' : '#D8543A'});
$("#resultCar").load("pag2.php?page=" + this.id, hideLoader);
return false;
});
$("#1").css({'background-color' : '#D8543A'});
showLoader();
$("#resultIndex").load("pag1.php?page=1", hideLoader);
$("#resultCar").load("pag2.php?page=1", hideLoader);
});
第 1 页.php
$word = mysql_real_escape_string(addslashes($_POST['word']));
echo "Word is: ".$word;
//here is the php and mysql querys to return the result
第 2 页.php
$word = mysql_real_escape_string(addslashes($_POST['word']));
echo "Word is: ".$word;
//here is the php and mysql querys to return the result
我感谢任何帮助。
不要使用内联函数,而是使用 jQuery 的本机方法:
$(function(){
$('#word').keyup(function(){
if(this.value) {
$.post("pag1.php", { word: value }, function(data) {
if(data){
$("#suggest").html(data);
} else {
$("#suggest").html("Theres nothing in DB!");
}
});
}
});
});
这里有一个示例场景
您可以为每个调用实现一个方法,并且在调用所有调用后,可以组合它们的结果
您还可以查看我的示例代码以重现您的场景(根据需要对其进行修改)
function getSugest(value){
if(value != ""){
if (callPage1(value) || callPage2(value)) {
var data1 = callPage1(value);
var data2 = callPage2(value);
//var combinedData = combine as data1 and data2 as you want
$("#sugest").html(combinedData);
} else {
$("#sugest").html("Theres nothing in DB!");
}
}
}
function callPage1(value) {
$.post("pag1.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
function callPage2(value) {
$.post("pag2.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}