我創建了一個簡單的應用程序,它連接到一個REST API,如下所示:div標籤未更新
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" >
</script>
<title></title>
</head>
<body>
<h1 id="header">A headline</h1>
<div id = "info"></div>
<p1 id = "p1">p1</p1>
<form id="name" name="name">
Please enter a switch: <input type="text" name="switch" id = "switch">
<input type="submit" value="submit" name="submit" id="submit">
</form>
<script>
$(document).ready(function() {
$('#name').submit(function() {
alert('submitted');
var switchName = $('#switch').val();
$.getJSON('http://localhost:8081/withindex/switch','name='+switchName, processSwitches(data));
});
});
function processSwitches(data) {
alert('processSwitches');
var infoHTML = '<p>Switch Name: ' + data.switchName +'</p>' ;
alert(infoHTML);
$('#info').html(infoHTML);
$('#header').html('<h1>Switches</h1>');
}
</script>
</body>
</html>
它運行,並將與infoHTML
字符串返回正確的警報,但我不能找出爲什麼它不會更新id爲info
的div標籤。
誰能幫助?
謝謝。
您是否嘗試刪除ID和名稱之間的空格? Like - > id =「info」 – Smartis
它們是id屬性和等號和屬性值之間的空格? – tschiela
你是不是阻止瀏覽器默認提交形式 – charlietfl