本文介绍了在函数之外询问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是jQuery的新手,想知道如何在此函数之外访问$ hidden: $(this).hide(500,function()
I'm new to jQuery and was wondering how I'd access $hidden outside of this function: $(this).hide(500, function ()
HTML
<div class="fl person">
<input type="hidden" name="userSaved" value="1" />
<img src="..." class="circle-mask" />
</div>
<div class="fl person">
<input type="hidden" name="userSaved" value="2" />
<img src="..." class="circle-mask" />
</div>
jQuery
<script>
$("div.person img").click(function () {
$(this).hide(500, function () {
$(this).parent("div").empty();
$(".main_page").appendTo("div.main_page").addClass("fl person");
var saved_id_user_who_voted = $hidden = $(this).siblings('input');
});
/* attach a submit handler to the form */
var saved_id_user_who_voted_val = "<?php echo $_SESSION['id']; ?>";
/* stop form from submitting normally */
event.preventDefault();
/* Send the data using post and put the results in a div */
$.ajax({
url: "saveSavedUserToDatabase.php",
type: "post",
data: {saved_id_user_who_voted:saved_id_user_who_voted_val, saved_id_user_voted_on:saved_id_user_voted_on_val}
});
});
</script>
我希望能够访问当前函数之外的 saved_id_user_who_voted ,以便我可以使用.ajax发布它 - 现在它不在范围内。
I'd like to be able to access saved_id_user_who_voted outside of the current function so that I can post it using .ajax - right now it's not in scope.
感谢任何帮助。
推荐答案
在中执行您的AJAX请求隐藏
回调。或者在隐藏
回调之外声明变量。
Execute your AJAX request in the hide
callback. Or declare the variable outside the hide
callback.
$("div.person img").click(function () {
$(this).hide(500, function () {
$(this).parent("div").empty();
$(".main_page").appendTo("div.main_page").addClass("fl person");
var saved_id_user_who_voted = $hidden = $(this).siblings('input');
/* attach a submit handler to the form */
var saved_id_user_who_voted_val = "<?php echo $_SESSION['id']; ?>";
/* stop form from submitting normally */
event.preventDefault();
/* Send the data using post and put the results in a div */
$.ajax({
url: "saveSavedUserToDatabase.php",
type: "post",
data: {
saved_id_user_who_voted: saved_id_user_who_voted_val,
saved_id_user_voted_on: saved_id_user_voted_on_val
}
});
});
});
这篇关于在函数之外询问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!