我正在學習ajax,而且我面臨着一個巨大的問題,我無法解決。我發送了一些變量到php中,我在那裏做了各種檢查,並將字符串值返回給ajax。我可以將這些顯示爲textare
一個(奇怪的結果即刻消失)和輸入字段。我想要做的就是將這些顯示到div
s。任何人都可以解釋我該怎麼做,我會在下面留下一些評論的代碼。謝謝! P.S這是關於爲用戶顯示註冊表單錯誤,也許我可以使用不同的方法?隨意提出一些建議。再次感謝!把json asnwer放入html標記
php.php
<?php
//various checking here
echo json_encode(array(
'email_error' => $email_error,
'password_error' =>$password));
?>
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<form>
Your email:
<input type = "text" id = "email"><br>
Your password:
<input type = "password" id = "password1"><br>
Repeat password:
<input type = "password" id = "password2"><br>
<button type = "submit" id = "push_button">PushMe</button>
</form>
<div id ="email_errors"> </div> <--I want to put variable $email_errors here -->
<div id = "password_errors"> </div> <--I want to put variable $password_errors here -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type = "text/javascript" ></script>
<script type="text/javascript" src="java_script.js"></script>
</body>
</html>
java_script.js
$(document).ready(function() {
$('#push_button').click(function() {
$.ajax({
url: "php.php",
type: "POST",
dataType: "json",
data: {
email: $("#email").val(),
password1: $("#password1").val(),
password2: $("#password2").val()
},
success: function(data) {
$("#email_errors").val(data.email_error); // i return these values (i tried html data type either
$("#password_errors").val(data.password_error);
}
});
});
})
什麼是JSON的樣子? – sbonkosky 2014-12-04 20:26:03
'div'沒有'value'屬性。你想使用'.html()' - >'$(「#email_errors」)。html(data.email_error); $(「#password_errors」)。html(data.password_error);' – Sean 2014-12-04 20:29:12
還是沒有,我可以看到我的字符串只有當我改變輸入字段的div。 – 2014-12-04 20:53:25