2013-01-06 15 views
1

發生了一件奇怪的事情,真的沒有一個想法如何解決這個問題。 Googleing時,我也沒有找到有用的東西。在FireFox中的JavaScript Array

我有一個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> 

然後用JavaScript(jQuery的)餘檢查哪個被檢查的,哪些不是:

var areas = []; 
$('input[name=areas]:checked').each(function(){ 
areas.push($(this).val()); 
}); 

這是從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(); 
    }); 
}); 

奇怪的是,whe在Firefox中,當我先前檢查了其中的一部分時,它們仍保留在區域數組中。即使我沒有選擇其中的一些,他們仍然在陣列中,反之亦然。但在Safari中進行調試時,它就像一個魅力!

當我然後取消選中所有東西時,數組爲空。重新檢查一些,然後在數組中。

所以,有什麼想法,火狐上發​​生了什麼?它看起來像FF正在緩存,即使在幾次刷新之後,也是前一個數組。儘管我

VAR區= []

中,我希望將其清空並重建....

這是活在here,在底部的大按鈕後解僱被點擊。

任何想法都更受歡迎!

+4

首先想到的是,發佈jsbin或鏈接到要調試的東西。 – vol7ron

+0

顯示更多代碼..什麼樣的處理程序調用此代碼?每次調用它時都會重置數組? – charlietfl

+3

您似乎沒有任何代碼可以對複選框的更改作出反應,並從數組中移除元素。 –

回答

1

問題出在您的事件處理程序上。問題是,你修改元素狀態的方式在Firefox中不起作用。您應該使用attr()而不是prop()來更改checked狀態。

工作代碼:代替.prop().attr()

//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(); 
}); 
+0

非常感謝,非常感謝您花時間來解決它。從來不知道它沒有奏效,謝謝! –

+0

樂意幫忙... – ATOzTOA

0

您正在將的值(這些是字符串)輸入到靜態數組中。沒有理由在Firefox中或任何其他瀏覽器中將此陣列與DOM一起更新。

如果您要將DOM元素本身存儲在數組中並從中檢索值,那將是一個不同的故事。