我在jquery中编写这些函数。我的一个功能是onclick,另一个是onready功能。我想在我的onready函数中使用这个变量“loadmorecount_rest_num”。如何在我的onready函数中使用此变量“loadmorecount_rest_num”?
我使用jquery编写这些脚本以便从php中的数据库加载更多值。如果数据是“var loadmorecount_rest_num< 0”.on ready函数,我想隐藏更多加载按钮。 现在我使用onclick事件隐藏了该按钮。我想在准备好的事件中隐藏它。
var STATUS= $('#status').val();
var TYPE= $('#Property').val();
var ADDRESS= $('#Address').val();
var MINA= $('#min_area').val();
var MAXA= $('#max_area').val();
var MINP= $('#min_price').val();
var MAXP= $('#max_price').val();
var MINIMUMBED= $('#minbed').val();
var MAXIMUMBED= $('#maxbed').val();
var MINBATHROOM= $('#minbath').val();
var MAXBATHROOM= $('#maxbath').val();
var total_data_count= $('#total_data_count').val();
var MINL = $("#minimum_limit").val();
var current_data_count = $("#page_data_count").val();
var MAXL = $("#maximum_limit").val();
var new_start_limit= parseInt(MAXL)+parseInt(MINL);
$("#minimum_limit").val(new_start_limit);
var data_start_limit_pass = $('#minimum_limit').val();
var data_max_limit_pass = $('#maximum_limit').val();
$.ajax({
type : 'POST',
url : '<?php echo MY_SITE_URL ;?>'+'webapi.php',
data :{task:'Properties.loadmore',MINL:new_start_limit,MAXL:MAXL,STATUS:STATUS,TYPE:TYPE,ADDRESS:ADDRESS,MINA:MINA,
MAXA:MAXA,MINP:MINP,MAXP:MAXP,MINIMUMBED:MINIMUMBED,MAXIMUMBED:MAXIMUMBED,MINBATHROOM:MINBATHROOM,MAXBATHROOM:MAXBATHROOM},
// data :{task:'Properties.loadmore',MINL:new_start_limit,MAXL:MAXL,get_data:get_data},
error: function() {
console.log('somthing wrong');
},
success: function(response) {
var json_data= JSON.parse(response);
var Properties=json_data.Response;
var count = json_data.Response.length;
var old_current_data_count = $('#page_data_count').val();
var new_current_data_count= parseInt(old_current_data_count)+ parseInt(count);
$('#page_data_count').val(new_current_data_count);
var html='';
$.each(Properties, function( key, value ) {
var nowDate = new Date();
var oneDay = 24*60*60*1000;
var secondDate = new Date(value.system_insert_date);
var diffDays = Math.round(Math.abs((nowDate.getTime() - secondDate.getTime())/(oneDay)));
// console.log(diffDays);
html = html+'<div class="listing-item">';
html = html+'<a href="#" class="listing-img-container">';
html = html+'<div class="listing-badges">';
html = html+'<span>'+value.current_status+'</span>';
html = html+'</div>';
html = html+'<div class="listing-img-content">';
html = html+'<span class="listing-price">$'+value.close_price+'<i>'+value.list_price_per_sqft+'/ sq ft</i></span>';
html = html+'<span class="like-icon"></span>';
html = html+'</div>';
html = html+'<img src="images/listing-05.jpg" alt="">';
html = html+'</a>';
html = html+'<div class="listing-content">';
html = html+'<div class="listing-title">';
html = html+'<h4><a href="#" class="listing-address popup-gmaps">'+value.street_number+','+value.street_name+'</a></h4>';
html = html+'<a href="https://maps.google.com/maps?q='+value.google_address+'" class="listing-address popup-gmaps">';
html = html+'<i class="fa fa-map-marker"></i>';
html = html+''+value.google_address+'';
html = html+'</a>';
html = html+'<a href="#" class="details button border">Details</a>';
html = html+'</div>';
html = html+'<ul class="listing-details">';
html = html+'<li>'+value.sqft+' Sqft</li>';
html = html+'<li>'+value.beds+' Beds</li>';
html = html+'<li>N/A</li>';
html = html+'<li>'+value.bathfull+' Baths</li>';
html = html+'<li>'+value.bathhalf+' Half Baths</li>';
html = html+'</ul>';
html = html+'<div class="listing-footer">';
html = html+'<a href="#"><i class="fa fa-user"></i>'+value.listing_office_name+'</a>';
html = html+'<span><i class="fa fa-calendar-o"></i>'+diffDays+' Days ago</span>';
html = html+'</div>';
html = html+'</div>';
html = html+'</div>';
});
$('#propertysearch').append(html);
$("#loadmore_btn").show();
$("#loadmorecount_rest").html('');
var loadmorecount_rest_num = parseInt(total_data_count)-(data_start_limit_pass)-(data_max_limit_pass);
$("#loadmorecount_rest").html(loadmorecount_rest_num);
var current_num_data=$('#page_data_count').val() ;
if(loadmorecount_rest_num==0 || loadmorecount_rest_num<0){
$("#loadmore_btn").hide();
}
else{
$("#loadmore_btn").show();
}
}
});
});
$(document).ready(function(){
});
答案 0 :(得分:1)
最好在准备好的功能中使用onclick功能。请看一下这个链接:Why should $.click() be enclosed within $(document).ready()?
你可以这样做:
$(document).ready(function(){
var loadmorecount_rest_num = 0;
$( "#loadmore_btn" ).on( "click", function() {
$("#loadmore_btn").show();
$("#loadmorecount_rest").html('');
loadmorecount_rest_num = parseInt(total_data_count)-(data_start_limit_pass)-(data_max_limit_pass);
$("#loadmorecount_rest").html(loadmorecount_rest_num);
var current_num_data=$('#page_data_count').val() ;
if(loadmorecount_rest_num==0 || loadmorecount_rest_num<0)
{
$("#loadmore_btn").hide();
}
else{
$("#loadmore_btn").show();
}
});
//You can use var loadmorecount_rest_num inside document ready now.
})
尝试使用函数包含代码,如下所示:
function yourFunction(loadmorecount_rest_num){
$( "#loadmore_btn" ).on( "click", function() {
$("#loadmore_btn").show();
$("#loadmorecount_rest").html('');
loadmorecount_rest_num = parseInt(total_data_count)-(data_start_limit_pass)-(data_max_limit_pass);
$("#loadmorecount_rest").html(loadmorecount_rest_num);
var current_num_data=$('#page_data_count').val() ;
if(loadmorecount_rest_num==0 || loadmorecount_rest_num<0)
{
$("#loadmore_btn").hide();
}
else{
$("#loadmore_btn").show();
}
});
}
$(document).ready(function(){
var loadmorecount_rest_num = 0;
yourFunction(loadmorecount_rest_num);
})
答案 1 :(得分:0)
如果没有深入了解你想要做什么,我可以建议的一件事是:
window.loadmorecount_rest_num = parseInt(total_data_count)-(data_start_limit_pass)-(data_max_limit_pass);
这会将loadmorecount_rest_num
附加到全局对象(窗口)。
这不是一个好习惯。您也可以从ready函数本身调用此单击侦听器函数函数,并将loadmorecount_rest_num
的值返回到ready函数并在任何您想要的地方使用它。
或者正如其他人建议在ready函数中声明loadmorecount_rest_num
变量并从ready函数中调用侦听器。这将保持loadmorecount_rest_num
的最后状态。
答案 2 :(得分:0)
你应该将你的功能包裹在$(document).ready
下
如果你想在这样的其他函数中使用这个变量,可以在函数范围之外定义一个全局变量。
$(document).ready(function(){
var loadmorecount =0;
$( "#loadmore_btn" ).on( "click", function() {
$("#loadmore_btn").show();
$("#loadmorecount_rest").html('');
loadmorecount_rest_num = parseInt(total_data_count)-(data_start_limit_pass)-(data_max_limit_pass);
$("#loadmorecount_rest").html(loadmorecount_rest_num);
var current_num_data=$('#page_data_count').val() ;
if(loadmorecount_rest_num==0 || loadmorecount_rest_num<0)
{
$("#loadmore_btn").hide();
}
else{
$("#loadmore_btn").show();
}
});
function someOtherFunction(){
//make use of global variable loadmorecount
}
someOtherFunction(); //call function
})
答案 3 :(得分:-1)
在顶部声明var loadmorecount_rest_num
,即之前
$( "#loadmore_btn" ).on( "click", function() { ...
这将使你的变量全局和你可以在其他功能中访问它。
-OR -
只需从变量loadmorecount_rest_num
中删除var,然后尝试访问另一个函数。