다소 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')와 같습니다. –