简体   繁体   English

如何解决jquery中的窗口加载事件问题?

[英]How to solve window load event issue in jquery?

i have used $(window).load in my project, when my page got loads first time, it works fine. 我在我的项目中使用了$(window).load ,当我的页面第一次加载时,它工作正常。 But when my page loads from cache or reload quickly then code written inside $(window).load function does not works. 但是当我的页面从缓存加载或快速重新加载时,编写在$(window).load函数内的代码不起作用。 How to solve this issue? 如何解决这个问题?

code in window.load window.load中的代码

$(window).load(function () {
    selectedfacode = '<?php echo isset($fmvrf_info)?$fmvrf_info["facode"]:''; ?>';
    if (selectedfacode > 0) {
        $('#facode option[value="' + selectedfacode + '"]').prop('selected', true);
    }
});
//also tried with
$("#facode").ajaxComplete(function () {
    selectedfacode = '<?php echo isset($fmvrf_info)?$fmvrf_info["facode"]:''; ?>';
    if (selectedfacode > 0) {
        $('#facode option[value="' + selectedfacode + '"]').prop('selected', true);
    }
});

You have a logic error where you compare if (selectedfacode > 0) because selectedfacode is defined as a string ( '' at a minimum, if no output returned from the PHP server script) and String > 0 will return false in JavaScript. 你有一个逻辑错误,你比较if (selectedfacode > 0)因为selectedfacode被定义为一个字符串( ''至少,如果没有从PHP服务器脚本返回的输出), String > 0将在JavaScript中返回false

I suggest you check the string's length instead. 我建议你检查字符串的长度。

You code before the fix: it will never select a value in #facode 您在修复之前编码:它永远不会在#facode选择一个值

 $(window).load(function() { selectedfacode = 'br'; if (selectedfacode > 0) { alert("WILL NEVER GET HERE"); $('#facode option[value="' + selectedfacode + '"]').prop('selected', true); } console.log("Can a string be > 0 in JavaScript? Answer: " + ("a string" > 0) + ""); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id='facode'> <option value='bl'>Blondes</option> <option value='br'>Brunettes</option> </select> 

Your code after the fix: checking the string length > 0 修复后的代码:检查字符串length > 0

 $(window).load(function() { selectedfacode = 'br'; if (selectedfacode.length > 0) { $('#facode option[value="' + selectedfacode + '"]').prop('selected', true); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id='facode'> <option value='bl'>Blondes</option> <option value='br'>Brunettes</option> </select> 

Next steps: after you fix this issue (assuming it was not an error introduced while copying and pasting into the question) if you still are having problems that vary between full load and cache hit it may be a timing issue. 后续步骤:修复此问题后(假设在复制和粘贴到问题时不是引入错误)如果您仍然遇到满负载和缓存命中之间的问题,则可能是时间问题。 For example, if you are loading the select element via AJAX you should obviously wait until it is loaded before trying to select an option, etc. 例如,如果您通过AJAX加载select元素,您应该在尝试选择选项之前等待它加载。

Change your technique as below to achieve what you want. 改变你的技术如下,以达到你想要的。 First of all set your variable value same as you did but instead of '' string set it as 0 for minimum. 首先设置您的变量值与您的变量值相同,但不是''字符串将其设置为0表示最小值。

$(window).load(function () {
    selectedfacode = '<?php echo isset($fmvrf_info)?$fmvrf_info["facode"]:0; ?>';
});

Now your ajax code should run to get your data for drop down like: 现在你的ajax代码应该运行以获取你的数据下拉,如:

$.ajax({
     type: "POST",
     data: "",
     url: "urlhere",
     success: function(result){
         //set your result as option list of your drop down as u explained above in comments.
         //here main work is call to a function which make an option selected as per the value you set in variable.
         setSelectedOpt();
     }
});

Your setSelectedOpt function here which will make an option as selected from options list based on your variable value. 这里的setSelectedOpt函数将根据您的变量值从选项列表中选择一个选项。

function setSelectedOpt(){
    //put if condition inside this function like:
    if( typeof selectedfacode !== 'undefined' && selectedfacode>0)
    {
     $('#facode option[value="' + selectedfacode + '"]').prop('selected', true);
     selectedfacode = 0;
    }
}

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

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