刷新后如何清除选择框的选项。。。我有两个选择框,刷新后它们的值都没有清除或重置我在代码点火器工作这是我的密码。
<?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);
});
}
});
});
});
// ]]>
尝试
$("#items").empty();
empty()
方法将清除所有html
API参考http://api.jquery.com/empty
而不是
$("#items > option").remove(); //it is not working
试试这个
$("#items).html("");
这里是简单的jsFiddle为你
更新:
您还可以考虑先构建选项标记,然后立即替换它,而不是按顺序追加项。
$("#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);
}
});
});
$('#items')
.find('option')
.remove()
.end();
IE6
$('#items')
.empty();
document.ready事件处理程序中没有任何内容。一切都包含在$('#category'(.change(function(({意味着页面刷新时不会发生任何事情。