我遇到selectpicker
的问题,当我尝试获取所选值时,在页面加载后我得到了值=“null”,但是当我尝试在所选更改后获取值时,我得到了正确的值,问题是什么?
我的代码是:
$( document ).ready(function() {
$('.selectpicker').selectpicker();
alert($("#level").val());
});
function Change(level){
alert($(level).val());
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.js"></script>
<select id="level" onchange="Change(this)" data-style="form-control" class="selectpicker" title="" name="level" style="width: 100%;">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
答案 0 :(得分:0)
这里你没有将任何值传递给函数onload,将其更改为this.val()
<select id="level" onchange="Change(this.val())" data-style="form-control" class="selectpicker" title="" name="level" style="width: 100%;">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>