<div class='titlewrap' data-status = 'public'>PUBLIC</div>
<div class='titlewrap' data-status = 'private'>PRIVATE</div>
<select id='selstatus'>
<option value = 'public'>PUBLIC</option>
<option value = 'private'>PRIVATE</option>
</select>
JS代码
我需要按所点击data status
的{{1}}的值更改所选选项。
像
这样的东西titlewrap
任何帮助?
答案 0 :(得分:1)
只需使用.val()
$('.titlewrap').click(function() {
var status = $(this).data('status');
$('#selstatus').val(status);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='titlewrap' data-status='public'>PUBLIC</div>
<div class='titlewrap' data-status='private'>PRIVATE</div>
<select id='selstatus'>
<option value = 'public'>PUBLIC</option>
<option value = 'private'>PRIVATE</option>
</select>