dongshen3352 2012-06-12 13:03
浏览 47
已采纳

onchange不工作jquery.selectbox

In onchange event is not working in jquery.selectbox-05, i am using two dropdown box one for country another one state if i select the country the state will pickup automatically it's work in normal php dropdown but i use this code in jquery.selectbox it's not working anybody will help me

Here my code

<script type="text/javascript" src="scripts/jquery.selectbox-0.5.js"></script>
<script type= "text/javascript" src = "scripts/countries2.js"></script>
$(document).ready(function(){
    $('.selectBoxMedium').selectboxMedium();
    $('.selectBoxSmall').selectboxSmall();
    $('.selectBoxSmallest').selectboxSmallest();
    $('.selectBox').selectbox();
    $(document).pngFix(); 
});
<p class="formInput formSelectBox">
    <select onchange="print_state1('state',this.selectedIndex);" id="country" class= "selectBox data" name ="country"></select>
</p>

<p class="formInput formSelectBox">
    <select name ="state" id ="state" class="selectBox"></select>
    <script language="javascript">print_country1("country");</script>
</p>

Javascript functions:

function print_country1(country_id) {
    // given the id of the <select> tag as function argument, it inserts <option> tags
    var option_str = document.getElementById(country_id);
    var x, i=0;
    for(x in country_arr){
        option_str.options[i++] = new Option(country_arr[x],country_arr[x]);
    }
}

function print_state1(state_id, state_index) {
    var option_str = document.getElementById(state_id);
    var x, i=0;
    state_index++;
    var state_arr = s_a[state_index].split("|");
    for(x in state_arr) {
        option_str.options[i++] = new Option(state_arr[x],state_arr[x]);
    }
}
  • 写回答

3条回答 默认 最新

  • duanchi4544 2012-06-12 14:13
    关注

    Here's your problem... this plugin don't handle with dynamic options. I mean, when you do $element.selectbox() the plugin executes with the actual options for the select element and will not update the selectbox if you changes the options.

    So, if you have a select element with no options and run the selectbox plugin, it will create a selectbox with no options elements. When you changes the country and do a "reload" in the options for select#states, all the states for the selected country are loaded and added as options for select#states element, but the plugin selectbox was executed in page load and will not reload the options in their elements :(

    How to fix it? In your case, you should be able to remove the siblings of select#state (generated by selectbox plugin), update the options with new state values and then run again the selectbox plugin :)

    Check this functional jsfiddle :)

    PS: this fiddle is working with countries-3.1.js, the jquery.selectbox-0.5 and no CSS

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏