简体   繁体   中英

How to put data in select option?

I am using tagsinput in my select option

<select multiple name="coupon[]" data-role="tagsinput" id="coupon" placeholder="Ketik Disini">
   <option></option>
</select>

When I put barcode qty, and then I click generate.. it will show the barcode as many as the barcode qty, but I only can show it in element p . I want that if I click generate, the barcode will show like the left picture. But I did not get like I want.

<a href="#" class="btn btn-primary" onclick="generate()">Generate Barcode Automatically</a>

This is my javascript:

function generate(){
    var total = document.getElementsByClassName('number')[0].value;
    var code = "";
    for(i = 0; i < total; i++){
        var generate = Math.floor(1000 + Math.random() * 9000) + '-' + Math.floor(1000 + Math.random() * 9000) + '-' + Math.floor(1000 + Math.random() * 9000) + '-' + Math.floor(1000 + Math.random() * 9000);
        code += generate + '</br>';
    }
    document.getElementById('isi').innerHTML = code;
    var opt = document.createElement('option');
    opt.setAttribute('value', code);
    opt.setAttribute('selected', 'selected');
    document.getElementById('coupon').appendChild(opt);
}

Add

opt.innerHTML = code;

underneath

opt.setAttribute('selected', 'selected');

Right now you are only setting the value, and not how it is displayed.

Also remove the </br> . Otherwise it will show up in the value attribute like so:

<option value="4008-1283-2298-5703</br>" selected="selected">4008-1283-2298-5703<br></option>

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