是否可以使用Jquery隐藏所有不在数组中的元素?
我在表单上有超过40个输入/选择,当我在表单上使用select时,我想隐藏其中的一些。
$("#type").on('change', function() {
if ($("#type").val() === "Blue") {
var elements = [$("#id-element"), $(".class-element), ... , ... ]; // these elements should be visible and the remaining elements on the form I want to hide them.
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='type'>
<option> Blue </option>
<option> Red </option>
</select>
答案 0 :(得分:0)
你可以使用jquery&#39; s not
$("#type").on('change', function() {
if ($(this).val() === "Blue") {
//note that this is a single string with comma sepreating between different selectors
var elements = "#id-element,.class-element";
//select all inputs
$("input").not(elements).hide();
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='type'>
<option> select one </option>
<option> Blue </option>
<option> Red </option>
</select>
<br />
<input id="id-element" value="#id-element" /><br />
<input class="class-element" value=".class-element" /><br />
<input id="other-id" value="#other-id" /><br />
<input class="other-class" value=".other-class" /><br />
&#13;