Javascript / jQuery - 从选项列表中添加类(下拉列表)

时间:2018-05-10 10:29:24

标签: javascript jquery onclick onchange

enter image description here 所以我在点击它时会有一个过滤器列表,它应该向img添加一个类,所以当点击过滤器brannan时,它应该将class='brannan'添加到img,选项和值与第一类相同。我如何知道addClass();图片,例如,如果我选择过滤器 brannan ,它应该将类Brannan添加到img。

<div class="form__field">
        <img id="preview" src="#" alt="Image preview" />
</div>

 <div class="form__field">    
        <select name="filter">
            <option value="_1977">1977</option>
            <option value="aden">aden</option>
            <option value="brannan">brannan</option>
            <option value="brooklyn">brooklyn</option>
            <option value="clarendon">clarendon</option>
        </select>

我假设代码会在select - &gt;上跟onchange类似。得到它的价值 - &gt; addClass();

5 个答案:

答案 0 :(得分:2)

addClass()事件中使用change()函数添加类。同时使用removeClass

删除课程

$("select[name='[filter]'").on('change',function(){
  var prevClass = $('img').attr('class');
  $('img').removeClass(prevClass);
  $('img').addClass($(this).val());
}); 
<div class="form__field">
        <img id="preview" src="#" alt="Image preview" />
</div>

 <div class="form__field">    
        <select name="filter">
            <option value="_1977">1977</option>
            <option value="aden">aden</option>
            <option value="brannan">brannan</option>
            <option value="brooklyn">brooklyn</option>
            <option value="clarendon">clarendon</option>
        </select>

答案 1 :(得分:1)

select上添加onchange侦听器,并将所选值作为类添加到您想要的div

&#13;
&#13;
$("select").change(function(){
     console.log("Adding class ",$(this).val())
    $("#preview").removeClass();
    $("#preview").addClass($(this).val())
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="form__field">
        <img id="preview" src="#" alt="Image preview" />
</div>
<select name="filter">
            <option value="_1977">1977</option>
            <option value="aden">aden</option>
            <option value="brannan">brannan</option>
            <option value="brooklyn">brooklyn</option>
            <option value="clarendon">clarendon</option>
        </select>
&#13;
&#13;
&#13;

注意:您不希望将之前添加的课程保留在img中,因此$("#preview").removeClass();将删除之前添加的所有课程并添加当前重要的课程

答案 2 :(得分:1)

使用jQuery changeaddClass()removeClass()尝试以下内容:

var classToRemove = '';
$('[name=filter]').change(function(){
  $('#preview').removeClass(classToRemove);
  $('#preview').addClass($(this).val());
  classToRemove = $(this).val();
  
  //For demonstration
  console.log($('#preview').attr('class'))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form__field">
  <img id="preview" src="#" alt="Image preview" />
</div>

<div class="form__field">    
<select name="filter">
  <option value="_1977">1977</option>
  <option value="aden">aden</option>
  <option value="brannan">brannan</option>
  <option value="brooklyn">brooklyn</option>
  <option value="clarendon">clarendon</option>
</select>

答案 3 :(得分:1)

您是否正确需要onChange功能,然后您可以将类设置为值的类:

&#13;
&#13;
var selectElem = document.querySelector('.form__field select');

selectElem.onchange = function() {
  document.querySelector('#preview').className = this.value;
}
&#13;
<div class="form__field">
        <img id="preview" src="#" alt="Image preview" />
</div>

<div class="form__field">    
        <select name="filter">
            <option value="_1977">1977</option>
            <option value="aden">aden</option>
            <option value="brannan">brannan</option>
            <option value="brooklyn">brooklyn</option>
            <option value="clarendon">clarendon</option>
        </select>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

最好使用input事件而不是change事件,以获得更多浏览器兼容性。

&#13;
&#13;
$( '[name=filter]' ).on( 'input propertychange', function() {
	$( '#preview' ).removeClass().addClass( ($( this ).val() ) );
	console.log( 'The image has a class named: ' + $( '#preview' ).attr( 'class' ) )
});
&#13;
#preview {
  width: 100px;
  height: 100px;
  border-width: 5px;
  border-style: solid
}
.default {
  border-color: black
}
.blue {
  border-color: blue
}
.yellow {
  border-color: yellow
}
.red {
  border-color: red
}
.green {
  border-color: green
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form__field">
	<img id="preview" src="https://www.gravatar.com/avatar/ef95d6eafc29a0cd76af27acc45da6dd?s=328&d=identicon&r=PG&f=1" alt="Image preview" class="default" />
</div>

<div class="form__field">    
	<select name="filter">
		<option value="default">Default</option>
		<option value="blue">Blue</option>
		<option value="yellow">Yellow</option>
		<option value="red">Red</option>
		<option value="green">Green</option>
	</select>
</div>
&#13;
&#13;
&#13;