admin管理员组

文章数量:1426066

I am using choose master /

Example


<select data-placeholder="Click to Select Role..." style="width:200px;"  id="geoRange" name="geoRange" class="chzn-select" tabindex="8" multiple>
<option value="1">England</option>
</select>

I am adding new option in this select


$('#geoRange').append( new Option('USA',2) );

It is not working

How can I add new option?

I am using choose master http://harvesthq.github./chosen/

Example


<select data-placeholder="Click to Select Role..." style="width:200px;"  id="geoRange" name="geoRange" class="chzn-select" tabindex="8" multiple>
<option value="1">England</option>
</select>

I am adding new option in this select


$('#geoRange').append( new Option('USA',2) );

It is not working

How can I add new option?

Share Improve this question edited Jul 27, 2015 at 8:04 Dimitri Dewaele 10.7k21 gold badges83 silver badges130 bronze badges asked Dec 30, 2012 at 10:14 Shahid GhafoorShahid Ghafoor 3,11318 gold badges73 silver badges126 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

After updating the select list, you apparently need to also notify Chosen that you did that.

$("#geoRange").trigger("liszt:updated");

I haven't used Chosen myself (using Select2 instead), but that's what the docs say. See http://harvesthq.github./chosen/

    <script type="text/javascript">
function fnc()
{
var select1 = document.getElementById("geoRange");
select1.options[select1.options.length] = new Option('USA', 2);
}
</script>
</head>

<body>
<select data-placeholder="Click to Select Role..." style="width:200px;"  id="geoRange" name="geoRange" class="chzn-select" tabindex="8" multiple>
<option value="1">England</option>
</select>
<input type="button" onclick="fnc()" value="click me" />
$('#geoRange').append( new Option('USA',2) );

The following statement solve my problem

 $("#geoRange").trigger("liszt:updated");

本文标签: javascriptdynamically add new option in jquery chosen plugin where select multipleStack Overflow