2016-01-29 76 views
2

我有用于流派过滤器的复选框列表。这里是HTMLJQuery复选框过滤器参数

<div class="gl-title">Türler</div> 
<ul class="check-list filters" id="genres"> 
    <?php foreach($genres as $genre){?> 
    <li> 
     <div class="customCheck"> 
     <input type="checkbox" data-genrename="<?php echo $genre->slug; ?>" id="tur_<?php echo $genre->term_id;?>"> 
     <label for="tur_<?php echo $genre->term_id;?>"></label> 
     </div> 
     <div class="cl-text"> 
     <?php echo $genre->name;?> 
     </div> 
    </li> 
    <?php }?> 
</ul> 
</div> 

在复选框,单击我要在url.Here的末尾添加genrename是我的jQuery代码

$(".customCheck").children("input").unbind(click).click(function(){ 
    if ($(this).is(":checked")) { 
     $(this).prop("checked", false); 
    } else { 
     $(this).prop("checked", true); 
    } 
    alert("blabla"); 
    var curUrl = window.location.href; 
    alert(curUrl); 

    if(curUrl.substring("?tur=")>-1){ 
     window.location=curUrl+"+"+$(this).attr("data-genrename"); 
    }else{ 
     window.location=curUrl+"?tur="+$(this).attr("data-genrename"); 
    } 
}); 

“TUR”在我的语意流派。(因为你可以了解网址。) 该功能在文件准备就绪。检查功能正在工作,但我不能看到点击时的警报框。我尝试使用隐身模式缓存没有任何变化。什么是问题非常感谢

+2

而你没有得到任何错误,比如'click is not defined',在'unbind(click)'这一行你缺少引号。 – adeneo

+0

哦,有时候答案很简单。我不知道我必须为事件使用引号。感谢您的关注。 –

+0

有没有什么理由在绑定之前解除绑定?似乎很奇怪。 –

回答

1

这可以是简单得多,修复一些语法问题,并与子问题被滥用,应该使用str.indexOf(searchValue[, fromIndex])代替。

注意我在此使用.data而不是.attr作为更好的方法。 !$(this).is(":checked")也可以!$(this)[0]checked甚至!this.checked

编辑:调整去除类的结合问题

$('#genres').on("change", "input[type='checkbox']", function() { 
    // set checkbox to what it is NOT (checked or unchecked (not sure why) 
    $(this).prop("checked", !$(this).is(":checked")); 
    alert("blabla"); 
    var curUrl = window.location.href; 
    alert(curUrl); 
    var turp = "?tur="; 
    if (curUrl.indexOf(turp) > -1) { 
    window.location = curUrl + "+" + $(this).data("genrename"); 
    } else { 
    window.location = curUrl + turp + $(this).data("genrename"); 
    } 
}); 

请注意,你也可以这样做:(但是这是更好的是有争议的)

var curUrl = (window.location.href.indexOf(turp) > -1) window.location.href + '+': window.location.href+"?tur="; 
window.location = curUrl + $(this).data("genrename"); 
2

无论何时您想使用复选框来玩,请始终使用.change()事件而不是.click()。

您可以在此时使用Ctrl + SpaceBar键选中或取消选中复选框.click()事件不会被触发。

$(document).on("change", ".customCheck :checkbox", function() { 
    if ($(this).is(":checked")) { 
     $(this).prop("checked", false); 
    } 
    else { 
     $(this).prop("checked", true); 
    } 
    alert("blabla"); 
    var curUrl = window.location.href; 
    alert(curUrl); 

    if (curUrl.substring("?tur=") > -1) { 
     window.location = curUrl + "+" + $(this).attr("data-genrename"); 
    } 
    else { 
     window.location = curUrl + "?tur=" + $(this).attr("data-genrename"); 

    } 

}); 
+0

非常感谢我接受这个答案在7分钟:) –

+0

它工作吗? –

+0

是的工作,但我有一个问题,如果协调不起作用它总是添加“?tur =”像这样“site.com/?tur=a?tur=b”而不是“?tur = a + b” –