简体   繁体   中英

how to change selected value of combobox?

I make a combobox.

I get data from database and set the value of firstname to combobox selected but it is not working. Here is my code:

 $('#invoices_invoicesbundle_invoicestype_firstname').val('{{firstname}}');

and here is my selectbox html:

<select id="invoices_invoicesbundle_invoicestype_firstname" required="required" name="invoices_invoicesbundle_invoicestype[firstname]">
    <option selected="selected" disabled="disabled">Please Choose</option>
    <option value="2">Ilyas</option>
    <option value="3">Arif</option>
    <option value="4">Ali</option>
    <option value="5">Arslan</option>
</select>

{{firstname}} value is Ali,how do I set Ali as selected?

You need to select an option by it's "value", not text node. Have a look at this and let me know if it helps: http://jsfiddle.net/4jGZD/

$('#invoices_invoicesbundle_invoicestype_firstname').val(4);

将{{firstname}}的值设置为4,而不是Ali

尝试不使用{{}}使用

$("#invoices_invoicesbundle_invoicestype_firstname").val("2");

you could always insert PHP CODE if possible and avoid using the Jquery

<select id="invoices_invoicesbundle_invoicestype_firstname" required="required"      name="invoices_invoicesbundle_invoicestype[firstname]">
<option disabled="disabled">Please Choose</option>
<option value="2" selected="selected"><?php echo $variable1DEFAULT ?></option>
<option value="3"><?php echo $variable2 ?></option>
<option value="4"><?php echo $variable3 ?></option>
<option value="5"><?php echo $variable4 ?></option>
</select>

i'm guessing something like that should work in your HTML

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