如何更改组合框的选定值

how to change selected value of combobox?

本文关键字:何更改 组合      更新时间:2023-09-26

我做了一个组合框。

我从数据库中获取数据并将名字的值设置为选定的组合框,但它不起作用。 这是我的代码:

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

这是我的选择框 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}}值为阿里,如何将阿里设置为选中?

您需要

通过"值"而不是文本节点选择一个选项。看看这个,让我知道它是否有帮助:http://jsfiddle.net/4jGZD/

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

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

尝试在没有 {{}} 的情况下使用

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

可能的话,你可以随时插入PHP CODE,避免使用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>

我猜这样的东西应该在你的 HTML 中工作