所以我有这个功能:
$(document).ready(function() {
var userInput = $("#userInput").val();
$.ajax({
url: 'foodstore.php',
data: userInput,
method: 'GET',
success: function(response){
$("#underInput").html(response);
}
});
});
php文件:
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = $_GET['food'];
$foodArray = array('tuna','bacon','beef','ham');
if(in_array($food,$foodArray)){
echo 'We do have '.$food.'!';
}elseif ($food==''){
echo 'Enter a food you idiot';
}else{
echo 'Sorry punk we dont sell no '.$food.'!';
}
echo '</response>';
?>
HTML文件:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.1.1.js"></script>
<script src="foodstore.js" type="text/javascript"></script>
</head>
<body>
<h3>The Chuff Bucket</h3>
Enter the food you would like to order:
<input type="text" id="userInput" />
<div id="underInput" />
</body>
</html>
当我尝试运行它时,我在控制台中出现错误: 未捕获的TypeError:无法读取null的属性'ownerDocument' 我知道我错过了什么,但我不知道是什么。 我正在学习JQuery,这对我来说有点模糊。