2016-09-24 1 views
-1

아래 스크립트가 .js 파일을 사용하여 포함되어있어 실행되지 않습니다. 나는 JQuery에 익숙하지 않아 어디서 잘못 될지 알 수 없다.checkbox onChange가 실행되지 않습니다

도와주세요. 내 개정을 데오빌로의 예제 코드를 사용하고는 제대로 감사 줄리안 $ (이) .attr 화재 :

는 이제 다음과 같이 변경하여 아래에 스크립트를 변경했습니다 ('확인') 작동하지 않습니다 나를 위해 그것은 "unasigned"를 반환합니다. 그러나 .prop를 사용하면 $ (this) .prop ('checked')가 예상대로 작동합니다. (true/false) 감사합니다. Rashet, 테스트 목적으로 URL을 간단한 테스트 스크립트가 들어있는 '/my.script.php'로 변경했습니다. 여전히 작동하지 않습니다.

if(isset($_POST['id']) || isset($_POST['Purchased'])){ 
die('Eureka! variables exist'); 
}else{ 
die('Failure!, variables do not exist'); 
} 

$('input[name=Purchased]').change(function(){ 
    //if a checkbox with name 'Purchased' is clicked, do the following. 
    var id=$(this).attr('id'); //grab the id from the clicked box 
    var Purchased=$(this).prop('checked'); 

    //alert("ID value:"+id); returns correctly 
// alert("Purchase value:"+Purchased); //Returns correctly 

    //setup the ajax call 
//Not posting the variable values to PHP processing script 
    $.ajax({ 
     type: 'POST', 
     url: '/my.script.php', 
     data: {id: 'id', 
     Purchased: 'Purchased'} 
    }); 
}); 
+0

의 URL이 잘못 그것을 보려면 여기를 클릭하십시오 그것에 대해 읽을 수 있어야합니다. 로컬 파일 시스템을 참조 할 수 없으며 현재 위치 위의 항목을 참조 할 수 없습니다. –

+0

코드에 php와 javascript가 모두 포함되어있는 경우 php 태그를 추가하십시오. –

답변

1

.is(selector)이 게시물 https://stackoverflow.com/a/12279447/5836034의 도움과 아래 Jquery doc on is() 실행 코드가 동작

$('input[name=Purchased]').change(function(){ 
 
    
 
    if (!$(this).is(":checked")) { 
 
     // not checked 
 
    alert("Not Checked"); 
 
     return; 
 
    } 
 
    
 
    alert("Checked continue"); 
 
    //check here 
 
    
 
    //if a checkbox with name 'Purchased' is clicked, do the following. 
 
    var id=$(this).attr('id'); //grab the id from the clicked box 
 
    var Purchased=$(this).val(); //grab the value from the checkbox (remember, if it is checked it will be 'on', else '' 
 

 
    alert("Purchase value:"+Purchased); 
 

 
    //setup the ajax call 
 
    $.ajax({ 
 
     type: 'POST', 
 
     url: '../../Includes/MyPHP.script.php', 
 
     data: {id: 'id', 
 
     Purchased: 'Purchased'} 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<input type="checkbox" value="value here" name="Purchased" >

+0

Theophilus, 예제 코드 덕분에 훨씬 더 명확 해졌습니다. –

+0

Owk는 도움이된다면 답장으로 표시 할 수 있습니다. 고맙습니다 –

0

$(this).val()$(this).prop('checked')해야하며 true 또는 false를 반환합니다.

+0

Jim 나는 $ (this) .attr (': checked')하지만 $ (this) .prop ('checked')가 작동하지 않는다는 것을 알았습니다. 당신의 도움을 주셔서 감사합니다. –

+0

아, 네 말이 맞아. 내 실수 야. 수정 된 답변 –

관련 문제