简体   繁体   中英

Jquery - convert object to string

I have this function:

function create_image(){
        <?php if(isset($avatar)) : ?>
            var brojac = 4;
        <?php else: ?>
            var brojac = 5;
        <?php endif; ?>
        var broj_slike = (5 - brojac) + 1,
        slike;
        for (var i = 0; i <= brojac; i++) {
            slike += $('<label for="image'+ broj_slike +'">Slika ' + broj_slike + '</label><input type="file" name="userfile" id="image' + broj_slike + '" />');
            broj_slike++;
        };
        return slike;
     }

It is returning this:

undefined[object Object][object Object][object Object][object Object][object Object]

How can I covert this into string?

If you want to deal with a string of HTML … don't convert each string to a jQuery object.

Get rid of $( and )

Why do you need to create jQuery object?

var broj_slike = (5 - brojac) + 1,
    slike = "";   // don't forget to init the string

for (var i = 0; i <= brojac; i++) {
    slike += '<label for="image'+ broj_slike +'">Slika ' + broj_slike + '</label> \
              <input type="file" name="userfile" id="image' + broj_slike + '" />';

    broj_slike++;
};

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