2012-08-11 104 views
0

我有这个脚本,我与我的标签一起使用,以便当用户切换标签时,如果选择前一个标签上的复选框,它们会变成未选中状态我的邮件系统)脚本选择所有输入,而不仅仅是复选框

然而,正如你所看到的,问题是该行

var w = document.getElementsByTagName('input'); 

选择所有的输入,这是导致我所有的文本框等...变成复选框,当我改变标签,有反正我只能选择复选框,而不是输入?

var w = document.getElementsByTagName('input'); 
     for(var i = 0; i < w.length; i++){ 
      if(w[i].type='checkbox'){ 
       w[i].checked = false; 

      }; 
     }; 

以下是更改的代码将删除选中的框,并且这是控制整个选项卡过程的整个代码;

$(document).ready(function() { 

    //Default Action 
    $(".tab_content").hide(); //Hide all content 
    $("ul.tabs li").eq(<?php print "$eq_id";?>).addClass("active").show(); //Activate second tab 
    $(".tab_content").eq(<?php print "$eq_id";?>).show(); //Show second tab content 

    //On Click Event 
    $("ul.tabs li").click(function() { 

     var w = document.getElementsByTagName('input'); 
      for(var i = 0; i < w.length; i++){ 
       if(w[i].type='checkbox'){ 
        w[i].checked = false; 

       }; 
      }; 

     $("ul.tabs li").removeClass("active"); //Remove any "active" class 
     $(this).addClass("active"); //Add "active" class to selected tab 
     $(".tab_content").hide(); //Hide all tab content 
     var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content 
     $(activeTab).fadeIn(); //Fade in the active content 
     return false; 
    }); 

}); 

感谢您的任何和所有帮助

回答

2

,因为你使用jQuery

推荐的方法

$('input[type=checkbox]') 

或两个弃用方式

$('input:checkbox') 
$(':checkbox') 

而且你是c把它们挂到复选框,在下面的语句

if(w[i].type='checkbox'){ // <-- you want == to compare and = to assign 

如果你只是使用jQuery ..你只需要选择并可以删除if语句一起

$('input[type=checkbox]').prop('checked',false); // <-- changes all checkboxes to unchecked 

http://jsfiddle.net/wirey00/UFdza/

以上应该在jQuery 1.6+中工作,但如果您仍在使用较旧的库,则可以使用以下内容:

$('input[type=checkbox]').attr('checked',false); 

http://jsfiddle.net/wirey00/UFdza/1/

EDIT--

您可以

$("ul.tabs li").click(function() {   
    $('input[type=checkbox]').prop('checked',false); // <-- changes all checkboxes to unchecked 
    $("ul.tabs li").removeClass("active"); //Remove any "active" class 
    $(this).addClass("active"); //Add "active" class to selected tab 
    $(".tab_content").hide(); //Hide all tab content 
    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content 
    $(activeTab).fadeIn(); //Fade in the active content 
    return false; 
}); 

是更换

$("ul.tabs li").click(function() { 

    var w = document.getElementsByTagName('input'); 
     for(var i = 0; i < w.length; i++){ 
      if(w[i].type='checkbox'){ 
       w[i].checked = false; 
      }; 
     }; 

    $("ul.tabs li").removeClass("active"); //Remove any "active" class 
    $(this).addClass("active"); //Add "active" class to selected tab 
    $(".tab_content").hide(); //Hide all tab content 
    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content 
    $(activeTab).fadeIn(); //Fade in the active content 
    return false; 
}); 

,作为Raminson指出:checkbox选择已被弃用。它不建议使用他们,但他们仍然可以工作

+2

请注意':checkbox'选择器是[弃用](http://api.jquery.com/category/deprecated/)。 – undefined 2012-08-11 17:45:48

+0

好的,编辑你刚刚做的,是否取代了我在原始文章中提供的全部代码?或者我用它替换了什么?感谢您的帮助 – Arken 2012-08-11 18:41:28

+0

我刚刚为您更新了答案。 @Raminson - 我将它添加到我的答案中。奇怪的是,它已被弃用,但它在':checkbox' jQuery文档中根本不存在。 http://api.jquery.com/checkbox-selector/ – 2012-08-11 18:47:20

0
$('input[type="checkbox"]').each(function(){ 
    $(this).removeAttr('checked'); 
}); 
1

应该不是你的条件是if (w[i].type == "checkbox") {而不是if (w[i].type = "checkbox") {? 如果您正在比较使用==

相关问题