【发布时间】:2015-07-22 01:59:45
【问题描述】:
我有三个文件。 test1.php、test2.php 和 ajax.php。在 test1.php 中,我有一个文本输入。当我在文本输入中键入内容时,它会调用 ajax 函数,并将我在文本输入中键入的任何内容作为 POST 传递给 test2.php。在 text2.php 中回显后,它显示在 test1.php 中的文本输入下方。
现在,我想在回显之前通过 ajax 函数 将字符串传递给 test2.php,因此它会显示在 test1.php 中的文本输入下方。例如,我想通过 ajax 函数将单词“Hello”传递给 test2.php - 我怎么能做到这一点,或者这甚至可能吗?谢谢你。
test1.php
<?php
include('ajax.php');
echo "<input type = 'text' name = 'text' onkeyup = 'ajax(\"test2.php\",\"input\",\"text\",\"output\")'>";
echo "<div id = 'output'/>";
?>
test2.php
<?php
$text = $_POST['text'];
echo $text;
?>
ajax.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function ajax(gotoUrl,type,inputName,output) {
$.ajax({
type: "POST",
url: gotoUrl,
data: { select: $(type+'[name='+inputName+']').val()},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( output ).innerHTML = data;
}
});
}
</script>
【问题讨论】:
-
我已经阅读了大约 5 次,但我不明白你的问题是什么或你想要做什么......
-
@DaveChen 我想在回显之前通过 ajax 函数将一个字符串传递给 test2.php,所以它会显示在 test1.php 中的文本输入下方。通过ajax函数,而不是通过文本输入。
-
@DaveChen 代码本身没有问题。我只是将它用作 ajax 函数可以做什么的示例。我的实际问题在第二段。
-
@Rasclatt 我想在回显之前通过 ajax 函数将字符串传递给 test2.php,因此它会显示在 test1.php 中的文本输入下方。通过ajax函数,而不是通过文本输入。
-
所以你有使用
keyup输入的ajax,但是你想让它使用字符串作为函数的参数之一自动加载(或以某种方式触发)?
标签: javascript php jquery ajax parameters