简体   繁体   中英

Using values from Array to execute function in jQuery

I have a page that contains several divs. The divs have the same postfixes but different prefixes. What I want to do is loop through them and if one of them contain a 0 hide the description div.

The divs look like this:

<div id="first_desc">First</div><div id="first_note1">0</div>
    <div id="second_desc">Second</div><div id="second_note1"></div>
    <div id="third_desc">Third</div><div id="third_note1"></div>

My script is this:

$(document).ready(function() {

// Variables
    var firstdiv = 'first';
    var seconddiv = 'second';
    var thirddiv = 'third';

// Array
var myArray = new Array(firstdiv, seconddiv, thirddiv);

for(var x = 0; x < myArray.length; x++) {

    if('$("#' + myArray[x] + '_note1").text() === $.trim("0")') {
        '$("#' + myArray[x] + '_desc").hide()';
}
}
});

So far this code is not working. Can anyone help me? Thanks.

You have some extra ' in there.

if($("#" + myArray[x] + "_note1").text() === $.trim("0")) {
    $("#" + myArray[x] + "_desc").hide();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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