如何将jQuery代码用于两个或更多选项?

时间:2016-12-29 01:12:36

标签: javascript jquery

如何将jQuery代码用于两个或更多选项? 我的代码:


   
jQuery(document).ready(function($) {
  jQuery('#select123' || '#select12').change(function() {
    jQuery('.content123' || '.content13').hide();
    jQuery('#' + jQuery(this).val()).show();
  }).trigger("change");
});
div.content123,div.content13 {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select123">
    	<option value="content1" selected="selected">content1</option>
    	<option value="content2">content2</option>	
    </select>

    <div id="content1" class="content123">
    	 text defult show
    </div>
    <div id="content2" class="content123">
    	contentttttttt
    </div>

<select id="select12">
    	<option value="content01" selected="selected">content1</option>
    	<option value="content02">content2</option>	
    </select>

    <div id="content01" class="content13">
    	 text defult show
    </div>
    <div id="content02" class="content13">
    	contentttttttt
    </div>

1 个答案:

答案 0 :(得分:2)

jQuery('#select123, #select12')将选择这两个元素。

多选择器API

https://api.jquery.com/multiple-selector/