我注意到您发布的代码中有两个错误。我评论了旧代码,所以你可以很容易地注意到变化。
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<SCRIPT>
function kin() {
//var kobj = document.getElementById("k"); // input Object
//var q = kobj.value;
var q = $("#k").val();
$.ajax({
type: "POST",
url: "zips.php",
//data:q,
data: "q="+q,
success: function(res) {
$("#result").append(res);
console.log(res);
}
});
}
</SCRIPT>
<INPUT type="text" autocomplete="off" value="" id="k"><input type="submit" value="search" onClick="kin()" data-role="button">
<DIV id="result"></DIV>
JQuery 允许您像这样重写您发布的代码:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<SCRIPT>
$(document).ready(function() {
$("#submit").click(function(e) {
e.preventDefault();
var q = $("#k").val();
$.ajax({
type: "POST",
url: "zips.php",
data: "q="+ q,
success: function(res) {
$("#result").append(res);
console.log(res);
},
});
});
});
</SCRIPT>
<INPUT type="text" autocomplete="off" value="" id="k"><input type="submit" id="submit" value="search" data-role="button">
<DIV id="result"></DIV>