Javascript Ajax JQuery成功函数不工作
我不确定出了什么问题,但出于某种原因,Ajax中的success函数没有调用该函数。我要求它在PHP完成后调用 对于PHP,我有Javascript Ajax JQuery成功函数不工作,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我不确定出了什么问题,但出于某种原因,Ajax中的success函数没有调用该函数。我要求它在PHP完成后调用 对于PHP,我有$test='Hi';echo json_编码($test) 以下是我的主页代码: <?php session_start(); if(!isset($_SESSION["b2_in"])){ header("Location: b2.php"); } ?> <script> $(document).r
$test='Hi';echo json_编码($test)代码>
以下是我的主页代码:
<?php
session_start();
if(!isset($_SESSION["b2_in"])){
header("Location: b2.php");
}
?>
<script>
$(document).ready(function(){
$("form input:submit").click(function() {
$.ajax({
type: "POST",
url: 'b2_send.php',
data: $('form').serialize(),
dataType: 'json',
//beforeSend: function(){ $("#send").val('Sending...');},
success: function(data) {
TestFunction();
},
statusCode: {
403: function(e) {
$("say").highlight();
$("#message").html(e.responseText);
}
}
});
return false;
});
});
function TestFunction(){
$("#message").val("");
}
</script>
<say>
<form>
<input type="text" name="message" class="Message" id="message"/>
<input type="submit" name="send" value='Say' id="send"/>
<span id="message" style="font-weight:bold;color:red;"></span>
</form>
</say>
$(文档).ready(函数(){
$(“表单输入:提交”)。单击(函数(){
$.ajax({
类型:“POST”,
url:'b2_send.php',
数据:$('form')。序列化(),
数据类型:“json”,
//beforeSend:function(){$(“#send”).val('Sending…');},
成功:功能(数据){
TestFunction();
},
状态代码:{
403:功能(e){
$(“say”).highlight();
$(“#message”).html(e.responseText);
}
}
});
返回false;
});
});
函数TestFunction(){
$(“#消息”).val(“”);
}
试试这个
span是DOM的一个块元素,因此要为其设置数据,需要使用$(id).html(data)代码>而非val()
此外,对于dom中的每个元素,您应该有不同的id,它总是选择它在dom中获得的第一个id,在您的例子中,它是
因此它将更改此元素的值
<script>
$(document).ready(function(){
$("form input:submit").click(function() {
$.ajax({
type: "POST",
url: 'b2_send.php',
data: $('form').serialize(),
dataType: 'json',
//beforeSend: function(){ $("#send").val('Sending...');},
success: function(data) {
TestFunction();
},
statusCode: {
403: function(e) {
$("say").highlight();
$("#message").html(e.responseText);
}
}
});
return false;
});
});
function TestFunction(){
$("#message").html("");
}
</script>
$(文档).ready(函数(){
$(“表单输入:提交”)。单击(函数(){
$.ajax({
类型:“POST”,
url:'b2_send.php',
数据:$('form')。序列化(),
数据类型:“json”,
//beforeSend:function(){$(“#send”).val('Sending…');},
成功:功能(数据){
TestFunction();
},
状态代码:{
403:功能(e){
$(“say”).highlight();
$(“#message”).html(e.responseText);
}
}
});
返回false;
});
});
函数TestFunction(){
$(“#消息”).html(“”);
}
$(“#消息”).val(“”)代码>实际上不会做任何可见的事情。那么您如何知道您的函数没有执行呢?对于span
,您应该使用$(“#message”).html(“您的文本”)
@Jivings当用户将数据放入#消息字段时,我希望他们的数据在输入发送到数据库后消失,输入发送到数据库,但字段没有重置。单击提交按钮后页面是否刷新?这可能会中断JavaScription的执行。如果认识到textbox和span具有相同的id,请尝试更改其中一个?页面上还有一个重复的id,这将使其无法正常工作。