0

我做了一个组合框。

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

 $('#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}}值为阿里,如何设置阿里为选中?

4

4 回答 4

2

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

$('#invoices_invoicesbundle_invoicestype_firstname').val(4);
于 2013-10-08T10:32:09.143 回答
1

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

于 2013-10-08T10:36:52.620 回答
0

尝试不使用 {{}}

$("#invoices_invoicesbundle_invoicestype_firstname").val("2");
于 2013-10-08T10:31:50.407 回答
0

如果可能,您总是可以插入 PHP 代码并避免使用 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 中起作用

于 2013-10-08T10:39:53.510 回答