How can I clear the options of select box after refresh ... I have two select boxes and both of their values after refresh didn't clear or reset i am working in code igniter here is my code.
<?php echo form_dropdown('cat_id', $records2, '#', 'class="cho" id="category"');?>
<script type="text/javascript">//
$(document).ready(function(){
$('#category').change(function(){
$("#items > option").remove(); //it is not working
var category_id = $('#category').val();
$.ajax({
type: "POST",
url: "testController/get_items/"+category_id,
success: function(items)
{
$.each(items,function(item_id,item_name)
{
var opt = $('<option />');
opt.val(item_id);
opt.text(item_name);
$('#items').append(opt);
});
}
});
});
});
// ]]>
try
$("#items").empty();
empty()
method will clear all html
API Reference http://api.jquery.com/empty
$('#items')
.find('option')
.remove()
.end();
IE6
$('#items')
.empty();
Instead of
$("#items > option").remove(); //it is not working
try this
$("#items).html("");
Here is simple jsFiddle for you
UPDATE:
You might also consider to build you options markup first and then replace it at once, rather then sequentially append items.
$("#category").change(function(){
var category_id = $("#category").val();
$.ajax({
type: "POST",
url: "testController/get_items/" + category_id,
success: function(items)
{
var options = "";
$.each(items, function(item_id, item_name)
{
options += "<option value=\"" + item_id + "\">" + item_name + "</option>";
});
$("#items").html(options);
}
});
});
There is nothing in the document.ready event handler. Everything is contained within $('#category').change(function(){ meaning nothing will happen on page refresh.
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.