我有一个使用jquery ui的表单
selectable
从表中填充列表的插件。我的表单还允许用户在改变主意时删除单个或多个列表项。此函数在IE和Firefox中工作。如果用户选择下载他们的列表,列表也会自动清除并重置表单。这也适用于ie和firefox。
最后,我添加了一个按钮,删除所有列表项,如果用户希望从fresh开始,则重置表单。此函数仅在Firefox中有效。在ie中,列表项从它们所在的字段中删除,但出于某种原因
$('#newgroups').removeData()
被忽略。我知道这一点是因为在.remove或.tofile之后,我可以添加一个与以前的组同名但不再存在的组。在.clear之后,尽管表单看起来相同,但使用以前使用的组名创建列表项失败。
下面是代码(我省略了与删除列表项或重置表单无关的函数体):
$(function(){
$('#selectable').selectable({
//rules for how table elements can be selected
});
$("input[name='makegroup']").live("click", function(event){
//adding list items
});
$("input[name='removegroup']").live("click", function(event){
event.preventDefault();
groups.msg();
groups.remove(); //works in FF and IE
});
$("input[name='cleargroups']").live("click", function(event){
event.preventDefault();
groups.msg();
return groups.clear(); //partially fails in IE
});
$("form#grouper").submit(function(){
return groups.tofile(); //works in FF and IE
});
groups={
grpmsg: $('#grpmsg'),
grpselections: $('#newgroups'),
grpname: $("input[name='newgroup']"),
filetext: $("input[name='filetext']"),
add: function(){
//add option to this.grpselections and set this.grpselections.data
//compares input data to $grpselections.data() for problems and throws error if necessary
},
remove: function(){
var vals= this.grpselections.val();//selected list items
for(var i in vals){
this.grpselections.data(vals[i]).removeClass('ui-selected chosen');
this.grpselections.removeData(vals[i]);
}
this.grpselections.find(':selected').remove();
this.grpname.focus();
},
clear: function(){ //identical to tofile method of resetting form
this.grpselections.empty();
this.grpselections.removeData();//DOES NOT WORK IN IE
$('#selectable td').removeClass('ui-selected chosen');
this.grpname.focus();
return true;
},
tofile: function(){
this.grpselections.select();
var gtxt='';
this.grpselections.find('option').each(function(i){
gtxt += $(this).text() + "\n";
});
if (gtxt.length === 0) {
this.msg('nonetofile');
return false;
}
this.filetext.val(gtxt);
//BELOW IS IDENTICAL TO clear function EXCEPT IT WORKS IN IE TOO
this.grpselections.empty();
this.grpselections.removeData();
$('#selectable td').removeClass('ui-selected chosen');
this.grpname.focus();
return true;
//END INTERESTING BIT
},
msg: function(msgtype){
//show error message specific to problem
},
addline: function(groupname){
//auxilliary to add function
}
};
});