特定のラジオボタンが選択されたときにdivを表示し、ラジオボタンの値に基づいてそのラジオボタンが「選択解除」された場合にそのdivを非表示にする、非常に単純なjQueryコードを探しています。
1つのフィルターのラジオボタンで機能させる方法を見つけましたが、2つのフィルターでそれを解決したいと思います
- 属するタイプに基づいて
- ........数量に基づく
最初のフィルター (タイプ) で機能するコードは次のとおりです。
<script>
var $filters = $("input:radio[name='type']");
var $categoryContent = $('#mainhide .mainresultbox');
var $errorMessage = $('#errorMessage');
$filters.click(function() {
// if any of the radio buttons for brand are checked, you want to show div's containing their value, and you want to hide all the rest.
$categoryContent.hide();
var $selectedFilters = $filters.filter(':checked');
if ($selectedFilters.length > 0) {
$errorMessage.hide();
$selectedFilters.each(function (i, el) {
$categoryContent.filter(':contains(' + el.value + ')').show();
});
} else {
$errorMessage.show();
}
});
</script>
ラジオ ボタンの HTML は次のとおりです。
<ul>
<li><label for="cdg"><input type="radio" id="cdg" name="type" value="brand1"> brand1</label></li>
<li><label for="cdh"><input type="radio" id="cdh" name="type" value="brand2"> brand1</label></li>
<li><label for="cdi"><input type="radio" id="cdi" name="type" value="brand3"> brand1</label></li>
</ul>
<ul>
<li><label for="cdj"><input type="radio" id="cdj" name="quantity" value="10">10</label></li>
<li><label for="cdk"><input type="radio" id="cdk" name="quantity" value="20">20</label></li>
<li><label for="cdl"><input type="radio" id="cdl" name="quantity" value="30">30</label></li>
</ul>
これがdivとその内容です
<div id="#mainhide">
<div>brand1 in stock - 10</div>
<div>brand1 in stock - 20</div>
<div>brand1 in stock - 30</div>
</div>
<div id="errorMessage">reset all fliters</div>
両方のフィルターを機能させるにはどうすればよいですか。
ありがとう、ナレシュ・カミレッディ