[英]Javascript select menu onchange when it loads
我正在使用選擇菜單:
<select id="form_frame1" name="frame1" onchange="getChart(this);">
<option value="area_chart_google" >Area Chart</option>
<option value="area_chart_2" selected="selected">Stacked Chart</option>
</select>
假設僅在更改選擇時才觸發getChart:
function getChart(selection) {
alert(selection.value);
//do something
}
問題是,一旦加載頁面,我就會得到: area_chart_google :甚至以前選擇的那個都不是! 所以有兩件事我不明白:
我剛發現此功能,我想這是問題所在:
jQuery(function() {
if (localStorage.getItem('form_frame1')) {
jQuery("#form_frame1 option").eq(localStorage.getItem('form_frame1')).prop('selected', true);
jQuery("#form_frame1 option").change();
}
jQuery("#form_frame1").on('change', function() {
localStorage.setItem('form_frame1', jQuery('option:selected', this).index());
});
});
如果您實際上在使用jQuery,那為什么不嘗試
$('#form_frame1').change(function(){
getChart($('#form_frame1').val());
});
刷新頁面時,它將自動選擇第一個選項。
請看這個小提琴。 http://jsfiddle.net/6H9dr/1/
如下使用
$(document).ready(function() {
$('#form_frame1').change(function() {
alert('Hi');
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.