jQuery .each loop string or object

谁说我不能喝 提交于 2019-12-11 12:54:29

问题


Why does this not work?

$( ["blog","user","forum"] ).each(function(num,opt) {
    if ( window.location.pathname.indexOf(opt) != -1 ) {
        $('#rb-' + opt).attr('checked','checked');
        return false;
      }
});

When I input $('#rb-blog').attr('checked','checked'); it works as expected?

console.log(typeof opt) produces string and the expected value.

--- UPDATE ---

I've just seen the html is being written to the page via ajax and is executed on .ready() :( Thanks for the help all, much appreciated.


回答1:


What the problem could be if the page is not fully loaded and #rb-blog is not available yet.

$(document).ready(function(){
    $( ["blog","user","forum"] ).each(function(num,opt) {
        if ( window.location.pathname.indexOf(opt) != -1 ) {
            $('#rb-' + opt).attr('checked','checked');
            return false;
        }
    });
});



回答2:


As already noted in the comments, return false will actually exit the "blog", "user", "forum" loop and thus stop checking the checkboxes once one pathname.indexOf condition is true.

Also you might want to add a console.log(window.location.pathname); to make certain this variable contains what you are checking on. Perhaps it is a casing issue?

If you want to know which literals are present in pathname you could use this:

var isPresent = [];
$( ["blog","user","forum"] ).each(function(num,opt) {
    if ( window.location.pathname.indexOf(opt) != -1 ) {
        $('#rb-' + opt).attr('checked','checked');
        isPresent.push(opt);
    }
});

If you just want to know if one of the literals is present in pathname:

var isAtLeastOneIsPresent = false;
$( ["blog","user","forum"] ).each(function(num,opt) {
    if ( window.location.pathname.indexOf(opt) != -1 ) {
        $('#rb-' + opt).attr('checked','checked');
        isAtLeastOneIsPresent = true;
    }
});



回答3:


Solution: the HTML content was not yet written to the page, so now we wait for ajax requests to complete, then we call the function to update the select value, like so...

// apply checked to the selected element
function setAsChecked() {
    $.each(["blog","user","forum"], function(num,opt){
        if (window.location.pathname.indexOf(opt) != -1) {
            $('#rb-' + opt, '.radio_menu').attr('checked','checked');
            return false;
        }
    });
}

// $('.radio_menu').ajaxStop(function(){
//  setAsChecked();
// });

// UPDATE! ajaxStop() catches all our ajax events - not cool. Instead, use when
$.when( $.ajax("") ).done(function(){
    setAsChecked();
});

Maybe that saves someone else a headache!

--- EDIT ---

BE WARNED! This solution caused a major headache when used with CakePHP. The layout disappeared when we called this function in the footer.

See this thread: CakePHP no layout on back and forward button



来源:https://stackoverflow.com/questions/12353597/jquery-each-loop-string-or-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!