代码之家  ›  专栏  ›  技术社区  ›  dnagirl

jquery$('selector').removedata()出现间歇性IE问题

  •  1
  • dnagirl  · 技术社区  · 14 年前

    我有一个使用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                
            }
        };
    
    });
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Mark Schultheiss    14 年前

    好吧,首先我承认我根本没有使用可选择的东西,所以既然这样,在黑暗中一枪,你能锁链吗?

    this.grpselections.empty().removeData();
    
        2
  •  0
  •   redsquare    14 年前

    这里没有足够的信息。尝试在代码中放置一个debugger;语句并打开ie8开发人员工具来调试脚本并查看发生了什么。