简体   繁体   English

变量不会在jquery click函数中更改

[英]Variable not changing inside jquery click function

I have a list of most 17 recent entries from a db table and wanna use jquery/ajax to have a next button that loads the next 17 entries when you click it. 我有一个数据库表中最近的17个条目的列表,想要使用jquery / ajax的next按钮在您单击它时加载接下来的17个条目。 It passes the 17 variable to the .load which works fine first time around (this is later used as mysql limit), but then I try to increase the start variable with 17 so that next time I click it would pass 34 to the .load (so it loads next 17 again) but this doesn't work, it simply loads same 17 again on 2nd, 3rd ect. 它将17变量传递到.load,该变量在第一次正常工作(以后用作mysql限制),但是随后我尝试将start变量增加为17,以便下次单击时将34传递给.load。 (因此它会再次加载下一个17),但这不起作用,它只是在第二,第三等上再次加载了相同的17。 click (so nothing changes). 单击(因此没有任何变化)。 Reading other questions this should be the right way to use global variables by setting it with var and using it without var inside the function. 阅读其他问题,这应该是通过使用var设置全局变量并将其在函数内不使用var的情况下使用全局变量的正确方法。

<script>  
var start = 17;  
var loadUrl = '<?php echo site_url('welcome/battles'); ?>';  
$("#latestbattlesnext").click(function () {  
    $('#left').load(loadUrl + "/" + start);  
    start = start + 17;  
});  
</script>  

Is your site being refresh on any of these loads? 您的网站是否在这些负载中刷新? Whatever is in the <script> tags is going to be refreshed as soon as the page it's loaded on is refreshed (obviously). <script>标记中的所有内容都将在刷新(显然)后立即刷新。

This is the right way to do it as far as I can tell. 据我所知,这是正确的方法。

Maybe double check this line: 也许仔细检查这一行:

$('#left').load(loadUrl + "/" + start);

To make sure it's not reloading any of the <script> on the page. 为了确保它没有重新加载页面上的任何<script>

The syntax is right, aren't you setting the start var inside the .load function? 语法是正确的,不是在.load函数中设置start var吗?

try changing the name of start inside the given script. 尝试在给定脚本内更改start的名称。

Try this method: 试试这个方法:

var start = 17;  
var loadUrl = '<?php echo site_url('welcome/battles'); ?>';  
$("#latestbattlesnext").click(function () {
    $.ajax({
        url: loadUrl + '/' + start,
        type: 'get',
        dataType: 'html',
        success: function(data){
            $('#left').html(data);
            start = start + 17;
        },
        error(jqXHR, textStatus, errorThrown){
            console.log(textStatus + ' -- ' + errorThrown);
        }
    });    
});

Increment the start variable either before the call to load or in the callback. 在调用加载之前或在回调中增加start变量。

var start = 17;  
var loadUrl = '<?php echo site_url('welcome/battles'); ?>';  
$("#latestbattlesnext").click(function () {  
    start = start + 17;
    $('#left').load(loadUrl + "/" + start);  
}); 

Or 要么

var start = 17;  
var loadUrl = '<?php echo site_url('welcome/battles'); ?>';  
$("#latestbattlesnext").click(function () {  
    $('#left').load(loadUrl + "/" + start, function() {
         start = start + 17;
    });
}); 

You can try to use the load callback 您可以尝试使用load回调

var start = 17;  
var loadUrl = '<?php echo site_url('welcome/battles'); ?>';  
$("#latestbattlesnext").click(function () {  
   $('#left').load(loadUrl + "/" + start,function(){start = start + 17;});  
});  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM