我有以下脚本
<script language="javascript">
$(document).ready(function(){
var str="";
var className="";
$(".question_one_select").click(function(){
//if($(this).find(':radio').is(":disabled")==false){
//call ajax here
val=$(this).find(':radio').val();
$.ajax({
url: "{{Request::root()}}/myprofile/ajax/myquiz?id="+val,
context: document.body
}).done(function(data) {
//$( this ).addClass( "done" );
if(data.trim()==2){
alert("Please , login and answer quiz correctly to win exciting prizes");
}
else if(data.trim()==1){
//append correct answer string
alert("Correct answer !! you earned some points");
$(this).closest('.question_title').find('.correct').show();
//this is not working
}
else {
alert("Oops!! wrong answer , better luck next time");
}
});
$(this).find(':radio').prop('checked',true);
$(this).closest('.question_title').find(':radio').remove();
//alert(className);
});
});
</script>
问题:当我收到回电并完成了#39;从ajax我需要对我试图调用的父div进行一些更改
$(this).closest('.question_title').find('.correct').show();
其中$(this)
应为$(".question_one_select").click(function(){
答案 0 :(得分:1)
$.ajax()中的context
选项用于指定应在回调方法中使用的上下文。
由于您将上下文作为document.body传递给ajax调用,因此回调内的this
将引用document.body
对象。所以将其更改为this
$(document).ready(function () {
var str = "";
var className = "";
$(".question_one_select").click(function () {
//if($(this).find(':radio').is(":disabled")==false){
//call ajax here
val = $(this).find(':radio').val();
$.ajax({
url: "{{Request::root()}}/myprofile/ajax/myquiz?id=" + val,
context: this
}).done(function (data) {
//$( this ).addClass( "done" );
if (data.trim() == 2) {
alert("Please , login and answer quiz correctly to win exciting prizes");
} else if (data.trim() == 1) {
//append correct answer string
alert("Correct answer !! you earned some points");
var className = ".correct";
$(this).closest('.question_title').find(className).show();
//this didnt refer to $(".question_one_select")
} else {
alert("Oops!! wrong answer , better luck next time");
}
});
$(this).find(':radio').prop('checked', true);
$(this).closest('.question_title').find(':radio').remove();
alert(className);
//(this).closest('.question_title').find(className).show();
});
});