脚本选择所有输入,而不仅仅是复选框


Script selects all inputs instead of just checkboxes

我有这个脚本,我将其与我的选项卡结合使用,以便当用户切换选项卡时,如果上一个选项卡上有选中的复选框,它们将变为未选中状态(它有助于我的消息传递系统)

但是,如您所见,问题在于该行

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;
    });
});

感谢任何和所有的帮助

因为你正在使用jquery。

推荐方式

$('input[type=checkbox]')

或两种已弃用的方式

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

此外,您将在以下语句中将它们更改为复选框

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/

编辑--

您可以替换

$("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;
});

$("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;
});

是的,正如Raminson指出的那样:checkbox选择器已被弃用。 不建议使用它们,但它们仍然有效

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

$('input[type="checkbox"]').each(function(){
    $(this).removeAttr('checked');
});
相关文章: