2013-10-08 31 views
0

我做了一个组合框。如何更改组合框的选定值?

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

$('#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}}值是阿里,我该如何设置阿里的选择?

+0

您需要映射阿里'4'莫名其妙,因为'4'是要选择,而不是'选项的值一个li'。所以要么你有Javascript中的映射,比如'{Ilyas:2,..........,Ali:4}',否则你必须遍历所有选项来确定正确的 – devnull69

+0

或者关于将“价值”映射到名称,例如

回答

1

集合{{姓}}值4,而不是阿里

0

尝试使用不{{}}

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

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

$('#invoices_invoicesbundle_invoicestype_firstname').val(4); 
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工作

相关问题