如何声明要在每个函数/事件中使用的jquery变量?我的剧本看起来很难看'在每个事件中重复声明var,如何改变所有' var并在文档中使用?
$(document).ready(function(){
var keyword = $("#autocomplete").val();
group = <?php echo $tb->group ?>;
uid = <?php echo $tb->uid ?>;
loadData(1, group, uid, '');
$('#btn_search').click(function(){
var keyword = $("#autocomplete").val();
group = <?php echo $tb->group ?>;
uid = <?php echo $tb->uid ?>;
loadData(1,group,uid,keyword);
});
$('body').on('click touchend', '#list-container .pagination li.go', function(){
var page = $(this).attr('p');
group = <?php echo $tb->group ?>;
uid = <?php echo $tb->uid ?>;
keyword = $("#autocomplete").val();
loadData(page,group,uid,keyword);
});
});
function loadData(page,group,uid,keyword){
$.ajax({
type: "POST",
url: "process.php",
data: {
page:page,
group:group,
uid:uid,
keyword:keyword
},
success: function(response){
$('#output').html(response.html);
}
});
}
答案 0 :(得分:0)
声明没有var关键字的变量 那么它将是一个围绕窗口对象
的全局变量 keyword = $("#autocomplete").val();
group = <?php echo $tb->group ?>;
uid = <?php echo $tb->uid ?>;
或者像这样使用
window.keyword = $("#autocomplete").val();
window.group = <?php echo $tb->group ?>;
window.uid = <?php echo $tb->uid ?>;
修改了你的代码
$(document).ready(function(){
keyword = $("#autocomplete").val();
group = <?php echo $tb->group ?>;
uid = <?php echo $tb->uid ?>;
loadData(1, group, uid, '');
$('#btn_search').click(function(){
loadData(1,group,uid,keyword);
});
$('body').on('click touchend', '#list-container .pagination li.go', function(){
var page = $(this).attr('p');
loadData(page,group,uid,keyword);
});
});
function loadData(page,group,uid,keyword){
$.ajax({
type: "POST",
url: "process.php",
data: {
page:page,
group:group,
uid:uid,
keyword:keyword
},
success: function(response){
$('#output').html(response.html);
}
});
}