我有一些粗糙和准备代码,让我基于两个单选按钮来过滤一些成果。 我竭力要增加第三个变种,将作为“显示所有”,即同时显示所有四个div的。 任何帮助将非常感激。
<p class="text1">Variants
<label>
<input type="radio" name="type" value="variant-2" id="type_0" checked="checked" />
Variant 1</label>
<label>
<input type="radio" name="type" value="variant-1" id="type_1" />
Variant 2</label>
</p>
<script>
$(document).ready(function(){
$("input[name$='type']").click(function(){
var value = $(this).val();
if(value=='variant-2') {
$(".variant-1").show();
$(".variant-2").hide();
}
else if(value=='variant-1') {
$(".variant-2").show();
$(".variant-1").hide();
}
});
$(".variant-1").show();
$(".variant-2").hide();
});
</script>
<div class="variant-1">Variant 1</div>
<div class="variant-2">Variant 2</div>
<div class="variant-1">Variant 1.1</div>
<div class="variant-2">Variant 2.1</div>
这个怎么样? http://jsfiddle.net/WyxmF/1
<p class="text1">Variants
<label>
<input type="radio" name="type" value="variant-2" id="type_0" checked="checked" />
Variant 1</label>
<label>
<input type="radio" name="type" value="variant-1" id="type_1" />
Variant 2</label>
<label>
<input type="radio" name="type" value="variant-all" id="type_all" />
All</label>
</p>
<div class="variant variant-1">Variant 1</div>
<div class="variant variant-2">Variant 2</div>
<div class="variant variant-1">Variant 1.1</div>
<div class="variant variant-2">Variant 2.1</div>
$("input[name$='type']").click(function() {
var value = $(this).val();
if (value == 'variant-2') {
$(".variant-1").show();
$(".variant-2").hide();
}
else if (value == 'variant-1') {
$(".variant-2").show();
$(".variant-1").hide();
}
else if (value == 'variant-all') {
$(".variant").show();
}
});
$(".variant-1").show();
$(".variant-2").hide();
尝试基于属性“阶级”这个解决方案的jsfiddle
HTML
<p class="text1">Variants
<label>
<input class="gr" type="radio" name="type" value="one" id="type_0" checked="checked" />
Variant 1</label>
<label>
<input class="gr" type="radio" name="type" value="two" id="type_1" /> Variant 2</label>
<label>
<input class="gr" type="radio" name="type" value="all" id="type_all" /> All</label>
</p>
</br>
</br>
</br>
<div class="variant-1 one all">Variant 1</div>
<div class="variant-2 two all">Variant 2</div>
<div class="variant-1 one all">Variant 1.1</div>
<div class="variant-2 two all">Variant 2.1</div>
JS
$(document).ready(function(){
$(".gr").click(function(){
var value = $(this).attr('value');
$('.all').hide();
$('.'+value ).show();
})
});
试试这个检查FIDDLE
$(document).ready(function() {
$("input[name$='type']").click(function() {
var value = $(this).val();
if (value == 'variant-2') {
$(".variant-1").show();
$(".variant-2").hide();
}
else if (value == 'variant-1') {
$(".variant-2").show();
$(".variant-1").hide();
}
else{
$(".variant-2").show();
$(".variant-1").show();
}
});
$(".variant-1").show();
$(".variant-2").hide();
});
<p class="text1">Variants
<label>
<input type="radio" name="type" value="variant-2" id="type_0" checked="checked" />
Variant 1</label>
<label>
<input type="radio" name="type" value="variant-1" id="type_1" />
Variant 2</label>
<label>
<input type="radio" name="type" value="variant-3" id="type_2" />
Show All</label>
</p>
<div class="variant-1">Variant 1</div>
<div class="variant-2">Variant 2</div>
<div class="variant-1">Variant 1.1</div>
<div class="variant-2">Variant 2.1</div>
更新后的代码
更好的方式来减少repition
$(document).ready(function() {
$("input[name$='type']").click(function() {
var value = $(this).val();
if(value == 'variant-3'){
$('[class*=variant]').show();
}
else{
$('[class*=variant]').hide();
$('.'+ value ).show();
}
}).click();
});
已更新FIDDLE
试试这个..它简化了您的if else
$(document).ready(function() {
$("input[name$='type']").click(function() {
var value = $(this).val();
if (value == 'showall') {
$('div[class^=variant]').show();
} else {
var $thisClass = $('div.' + value);
$thisClass.show();
$('div[class^=variant]').not($thisClass).hide();
}
});
$("input[value=variant-1]").click();
});
小提琴