내 전역 변수에 문제가있어 도움을받을 수 있기를 바랍니다.자바 스크립트 결과의 전역 변수는 항상 정의되지 않음
<li>
<a href="<?php echo site_url("adminController/questionAdd/".$row->subjectid); ?>" id="<?php echo $row->subjectid; ?>" class="subject">Add Question</a>
</li>
가 지금은 자바 스크립트 click()
에 의해이 줄 내 ID를 통과하는 선에서
$(document).ready(function() {
var correctAnswer;
var subId;
$(".subject").click(function() {
subId = ($(this).attr('id')); //passed the id variable into the global variable
alert(subId) // when I alert this it returns the value
});
가 지금은이 라인에서 동일한 $ (문서) .ready (함수를 전역 변수를 사용()
$('#form-user').submit(function(e){
e.preventDefault();
var me = $(this);
var correct = correctAnswer;
var passedSubId = subId; // passed the global variable to this local variable
console.log(correct); // this is okey
console.log(subId); // this is undefined
});
결과
i
undefined
'document ready'와'form submit'을 정확히 닫았습니까? –
답변 해 주셔서 감사합니다. 예, 올바르게 닫았습니다. –