Javascript 获取父函数jquery的上下文
我有以下脚本Javascript 获取父函数jquery的上下文,javascript,jquery,object,jquery-callback,Javascript,Jquery,Object,Jquery Callback,我有以下脚本 <script language="javascript"> $(document).ready(function(){ var str=""; var className=""; $(".question_one_select").click(function(){ //if($(this).find(':radio').is(":disabled")==fals
<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>
其中
$(this)
应该是$(“.question\u one\u select”)的上下文。单击(function(){
中的上下文
选项用于指定回调方法中应使用的上下文
由于您将上下文作为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();
});
});
将
context:document.body
更改为context: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();
});
});