FireFox中的javascript数组

FireFox中的javascript数组,javascript,jquery,firefox,variables,Javascript,Jquery,Firefox,Variables,发生了一件奇怪的事情,真的不知道如何解决这个问题。我在谷歌搜索时也没有找到有用的东西 我有一个html表单,其中包括: <label for="gebied">Gebieden</label> <div class="button button-selected"><input type="checkbox" name="areas" value="nederland" checked="checked" />Nederland</div&g

发生了一件奇怪的事情,真的不知道如何解决这个问题。我在谷歌搜索时也没有找到有用的东西

我有一个html表单,其中包括:

<label for="gebied">Gebieden</label>
<div class="button button-selected"><input type="checkbox" name="areas" value="nederland" checked="checked" />Nederland</div>
<div class="button button-selected"><input type="checkbox" name="areas" value="europa" checked="checked" />Europa</div>
<div class="button button-selected"><input type="checkbox" name="areas" value="wereld" checked="checked" />De Wereld</div>
这是从
createShortUrl()中调用的,在相关代码的下面:

$(function() {
    //Handle things when a buttons is clicked
    $("div.button").click(function() {
        //Find the input field for the clicked div
        var input = $(this).find(':input');
        var inputName = $(input).attr('name');
        //Handle checkboxes, which define the gebied
        if ($(input).is(':checkbox')) {
            //Change the classes
            input.prop('checked', !input[0].checked);
            $(this).toggleClass('button-selected');
        }
        //Handle radio
        if ($(input).is(':radio')) {
            $('form').find('input[name=' + inputName + ']').each(function() {
                $(this).parent('div').toggleClass('button-selected');
                $(this).prop('checked', !input[0].checked);
            });
        }
        //Clicking means something chanhes; create a new short url
        createShortUrl();
    });
}); 
奇怪的是,在Firefox中,当我早些时候检查了其中一些时,它们会留在区域数组中。即使我取消选中其中一些,它们也会留在数组中,反之亦然。但在Safari中调试时,它就像一个符咒

然后取消选中所有内容时,数组为空。重新检查一些,然后在数组中

那么,有什么想法吗,Firefox到底怎么了?看起来FF正在缓存前一个数组,即使在刷新了几次之后也是如此。尽管我

变量区域=[]

我希望清空它并重建它

它是实时的,在点击底部的大按钮后触发

欢迎有任何想法

您正在将输入的值(这些是字符串)推送到一个静态数组中。在Firefox或任何其他浏览器中,没有理由将此数组与DOM一起更新

如果要将DOM元素本身存储在数组中并从中检索值,则情况就不同了。

将输入的值(这些是字符串)推送到静态数组中。在Firefox或任何其他浏览器中,没有理由将此数组与DOM一起更新


如果要将DOM元素本身存储在数组中并从中检索值,则情况会有所不同。

问题在于事件处理程序。问题是,您修改元素状态的方式在Firefox中不起作用。您应该使用
attr()
而不是
prop()
来更改
选中的状态

工作代码:
.prop()
替换为
.attr()


问题在于事件处理程序。问题是,您修改元素状态的方式在Firefox中不起作用。您应该使用
attr()
而不是
prop()
来更改
选中的状态

工作代码:
.prop()
替换为
.attr()


首先,发布jsbin或链接到要调试的内容。显示更多代码。。什么样的处理程序调用此代码?数组是否在每次调用时都会重置?您似乎没有任何代码对复选框的更改作出反应并从数组中删除元素。是否使用更改事件对更改作出反应@Dennishunik,从调试的角度来看,这不是很有帮助。你能用相关的代码进行JSFIDLE演示吗?首先,发布jsbin或链接到要调试的东西。显示更多代码。。什么样的处理程序调用此代码?数组是否在每次调用时都会重置?您似乎没有任何代码对复选框的更改作出反应并从数组中删除元素。是否使用更改事件对更改作出反应@Dennishunik,从调试的角度来看,这不是很有帮助。您能用相关代码进行JSFIDLE演示吗?非常感谢,非常感谢您花时间来解决这个问题。顺便说一句,我从来不知道它不起作用,谢谢!非常感谢,非常感谢你花时间解决这个问题。顺便说一句,我从来不知道它不起作用,谢谢!
$(function() {
    //Handle things when a buttons is clicked
    $("div.button").click(function() {
        //Find the input field for the clicked div
        var input = $(this).find(':input');
        var inputName = $(input).attr('name');
        //Handle checkboxes, which define the gebied
        if ($(input).is(':checkbox')) {
            //Change the classes
            input.prop('checked', !input[0].checked);
            $(this).toggleClass('button-selected');
        }
        //Handle radio
        if ($(input).is(':radio')) {
            $('form').find('input[name=' + inputName + ']').each(function() {
                $(this).parent('div').toggleClass('button-selected');
                $(this).prop('checked', !input[0].checked);
            });
        }
        //Clicking means something chanhes; create a new short url
        createShortUrl();
    });
}); 
//Handle things when a buttons is clicked
$("div.button").click(function() {
    console.log("Click");

    //Find the input field for the clicked div
    var input = $(this).find(':input');
    var inputName = $(input).attr('name');

    //Handle checkboxes, which define the gebied
    if ($(input).is(':checkbox')) {
        //Change the classes
        console.log("check");
        input.attr('checked', !input[0].checked);
        $(this).toggleClass('button-selected');
    }

    //Handle radio
    if ($(input).is(':radio')) {
        $('form').find('input[name=' + inputName + ']').each(function() {
            $(this).parent('div').toggleClass('button-selected');
            $(this).attr('checked', !input[0].checked);
        });
    }
    //Clicking means something chanhes; create a new short url
    createShortUrl();
});