简体   繁体   中英

JQuery Append to SELECT not working?

I'm attempting to append an item to my jquery select like so...

http://jsfiddle.net/5sWCp/

but it doesn't appear to be working. could anyone tell me what I'm doing wrong? If it's at all relevant I'm testing on Chrome.

You didn't select jQuery in jsFiddle, you were using MooTools so it is not surprising that your jsfiddle doesn't work.

I've fixed it here: http://jsfiddle.net/5sWCp/1/

Also notice that you should use $("<option>").attr("value", msg) and not $("option").attr("value", msg) as you did in your initial fiddle. So here's the final version:

function append(msg) {
    $('<option/>')
        .attr('value', msg)
        .text(msg)
        .appendTo('#n');
}

append('test');

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