开发者

jquery counting function. if statement

开发者 https://www.devze.com 2023-01-17 00:59 出处:网络
I am trying to write a bit of jquery to: 开发者_运维百科if there is only one instance of the div which has a class of \"option\" take the href attribute and redirect to that href, this is what i have

I am trying to write a bit of jquery to: 开发者_运维百科 if there is only one instance of the div which has a class of "option" take the href attribute and redirect to that href, this is what i have to far

<script type="text/javascript">

var count =('Size: ' + $('.option').size());

if (count = 1) {

    $('.readmore a').each(function () {
        var url = $(this).attr('href');
        window.location.replace(url);
    })              

} 
else {

   alert("more than 1");    
}
</script>

It successfully redirects to the correct url, but it doesnt work properly because it if there are more than 1 instance of the div.option then it still redirects and doesnt alert "more that 1" anyone help?

im guessing at one point of the script the count is = to 1 and the jquery beings to does its redirect, but I want it to count all the instances then determine to redirect or not


You are setting count to 'Size: ' + $('.option').size(), which makes it a string. Then you are comparing it to an int. Also, if (count = 1) should be if (count == 1). Notice the two equal signs.


if (count = 1) is always true, since it sets count equal to 1!. if (count == 1) will only be true if count is equal to 1. The two don't have to be of the same type. if (count === 1) will only be true if the two are equal and of the same type.

Count should be how many there are, not a string containing "Size: ...".

You should use the length property instead of .count() since it's slightly faster (this is according to jQuery).

You could wrap the whole thing into a self calling anonymous function, so that you don't pollute the global namespace.... unless you make use of these variables later on (obviously only in the case where you don't redirect the page).

I'm assuming the HTML is before the <script> tags.... if not, you need a doc ready: `$(function() { ... });

<!-- HTML up here ... -->
<script type="text/javascript">
(function() {
var count =($('.option').length);

// "count = 1" SETS COUNT EQUAL TO 1!!!!!
if (count === 1) {

      // Why use each? We are redirecting to the first href.
    $('.readmore a').each(function () {
        var url = $(this).attr('href');
        window.location.replace(url);
    })              

} 
else {

   alert("more than 1... specifically " + count);    
}
})();
</script>

Finally, there is no reason to use .each() since you will redirect after the first .readmore a.... Maybe you should be checking how many .readmore as there are? At any rate this makes more sense to me:

<!-- HTML up here ... -->
<script type="text/javascript">
(function() {
var count =($('.option').length);

// "count = 1" SETS COUNT EQUAL TO 1!!!!!
if (count === 1) {

    var url = $('.readmore a').first().attr('href');
    window.location.replace(url);

} 
else {

   alert("more than 1... specifically " + count);    
}
})();
</script>

jsFiddle example w 2

jsFiddle example w 1


if you are comparing numbers it's better to use === "strict comparison operator"

if ( $('.option').length === 1 ){
        window.location.replace( $('.readmore a').attr('href') );
}
else{
    alert("more than 1");   
}
0

精彩评论

暂无评论...
验证码 换一张
取 消