JS/jQuery有點新鮮,但我試圖將隱藏字段的值更改爲JSON對象特定值的總和,因爲它是循環的。這裏是我有:jQuery .val()問題
<form>
<input id="totDistance" type="hidden" value="" />
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "loadPolys.php",
dataType: "json",
success: function(returnedJson){
jQuery.each(returnedJson, function(i, val) {
var curType = val.type;
var curAlias = val.alias;
var curDistance = val.distance;
totDistance += parseInt(curDistance, 10);
$('#totDistance').val(totDistance);
});
},
error: function(){
alert("oh no");
}
});
});
</script>
儘管輸入字段保持被設置爲「[object HTMLInputElement] 1734」。要添加的值是17和34,所以數據正在被拖動...並且totDistance設置爲51 ...我做錯了什麼? noobie
凡'totDistance'定義? –
這解決了這個問題。一開始我在所有函數之外定義了totDistance,但是在ajax回調函數內部定義了它,這是奇蹟。 – josephndenton
@KevinB我認爲這是特定於IE的問題..其中'totDistance'等同於'docuement.getElementById('totDistance')' –