2014-12-22 109 views
1

這是我的代碼。此代碼不適用於Mozilla Firefox瀏覽器。複選框更改事件不工作在Firefox中jquery

我的HTML

<input class="staff_check" name="select_staff" type="checkbox" value="@Model.Caregivers[i].AgencyUserID" /> 

腳本

 var selected_staff = []; 
     var selected_staff_names = []; 
     $('input[name="select_staff"').change(function() { 
      console.log('ds'); 
      if (this.checked) { 
       selected_staff.push($(this).val()); 
       selected_staff_names.push('<span class="selected_staff">' + $(this).parent('label').text().trim() + '</span>'); 
      } 
      else { 
       var idx = $.inArray($(this).val(), selected_staff); 
       if (idx > -1) { 
        selected_staff.splice(idx, 1); 
        selected_staff_names.splice(idx, 1); 
       } 
      } 
     }); 

這在谷歌瀏覽器工作正常。但它不適用於Mozilla Firefox。

+0

的http://的jsfiddle。 net/QnFg3/1/ – Manoj

+0

http://jsfiddle.net/stackmanoj/gj6ramg2/6/ @Mano z – Manoj

回答

2

你錯過右方括號 ']',更改:

... 
$('input[name="select_staff"').change(function() { 
.... 

... 
$('input[name="select_staff"]').change(function() { 
... 
+0

感謝它的工作 – Manoj

1

代替change()功能,嘗試click()

var selected_staff = []; 
     var selected_staff_names = []; 
     $('input[name="select_staff"]').click(function() { 
      console.log('ds'); 
      if (this.checked) { 
       selected_staff.push($(this).val()); 
       selected_staff_names.push('<span class="selected_staff">' + $(this).parent('label').text().trim() + '</span>'); 
      } 
      else { 
       var idx = $.inArray($(this).val(), selected_staff); 
       if (idx > -1) { 
        selected_staff.splice(idx, 1); 
        selected_staff_names.splice(idx, 1); 
       } 
      } 
     }); 
+0

這不適合我 – Manoj

+0

它爲我工作,謝謝。更改事件在Firefox 39.0(Linux)上不起作用。 – juanra