【问题标题】:onchange not working jquery.selectboxonchange 不工作 jquery.selectbox
【发布时间】:2012-06-12 13:03:09
【问题描述】:

在 onchange 事件在 jquery.selectbox-05 中不起作用,我使用两个下拉框,一个用于国家另一个州,如果我选择国家,该州将自动拾取它在正常的 php 下拉列表中工作,但我使用此代码jquery.selectbox 它不起作用任何人都会帮助我

这是我的代码

<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 函数:

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]);
    }
}

【问题讨论】:

  • 代码在哪里?粘贴您的代码(HTML 和 JavaScript):D
  • 粘贴函数print_state1print_country1的代码?函数print_country1被正确执行了吗?
  • function print_country1(country_id){ // 给定
  • 我从这个站点获取代码bdhacker.wordpress.com/2009/11/21/…
  • @vicky,你从哪里得到选择框插件?

标签: php jquery jquery-plugins


【解决方案1】:

这是你的问题...这个插件不处理动态选项。我的意思是,当您执行$element.selectbox() 时,插件会使用选择元素的实际选项执行,并且如果您更改选项,则不会更新选择框。

因此,如果您有一个 没有选项 的选择元素并运行 selectbox 插件,它将创建一个没有选项元素的选择框。当您更改国家并在select#states 的选项中执行“重新加载”时,所选国家的所有州都会被加载并添加为select#states 元素的选项,但插件selectbox 在页面加载和不会重新加载其元素中的选项:(

如何解决?在您的情况下,您应该能够删除 select#state 的兄弟姐妹(由选择框插件生成),使用新状态值更新选项,然后再次运行选择框插件:)

检查这个功能jsfiddle :)

PS:这个小提琴使用countries-3.1.jsjquery.selectbox-0.5,没有CSS

【讨论】:

  • 非常感谢它工作正常,但 js 在同一页面中实现,再次非常感谢您
  • @vicky 我在同一页面中添加了 JS,因为我找不到指向 .js 文件的直接链接,只有一些 zip 和 jsfiddle 不允许我上传新的js 文件:\ 对于您的情况,您仍然应该从远程文件调用 js 脚本,并将依赖于 DOM 的 JS 操作转换为 $.ready :)
【解决方案2】:

这个插件的问题是它不能处理 onchange() 事件,因为我们很生气。所以,这是解决方案..

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="jquery.selectbox-0.5.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('.items').selectbox();
                $('#country_input').blur(function(){
                   setTimeout(function(){
                       var e = document.getElementById("country");
                       var countryCode = e.options[e.selectedIndex].value;
                       getState('state',countryCode);
                   },500)                        

                });
});

</script>

<script type="text/javascript">
function getState(state_id, countryCode)
{
var option_str = document.getElementById(state_id);
option_str.length=0;
option_str.options[0] = new Option('Select State','');
option_str.selectedIndex = 0;
//write your code to get state list with the use of "countryCode"
var state_arr = stateListArray; //assume "stateListArray" is an array which is holding list of states
for (var i=0; i<state_arr.length; i++) {
    option_str.options[option_str.length] = new Option(state_arr[i],state_arr[i]);
}
$('#state').parent().children(':not(#state)').remove();
$('#state').selectbox();
}
</script>

<p>
<select id="country" class= "items" name ="country">
<option value="countryCode">Country Name</option>
</select>

【讨论】:

    【解决方案3】:
    <script type="text/javascript" src="scripts/jquery.selectbox-0.5.js"></script>
    <script type="text/javascript" src = "scripts/countries2.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('.selectBoxMedium').selectboxMedium();
        $('.selectBoxSmall').selectboxSmall();
        $('.selectBoxSmallest').selectboxSmallest();
        $('.selectBox').selectbox();
        $(document).pngFix();
    
        $('.selectBoxMedium, .selectBoxSmall, .selectBoxSmallest, .selectBox').change(function() {
            alert($(this).attr('class') + ' has changed');
        });
    
        $('#country').change(function() {
            print_state1('state', $(this+':selected').index());
        });
    
        print_country1("country");
    });
    </script>
    <p class="formInput formSelectBox">
        <select id="country" class="selectBox data" name="country"></select>
    </p>
    
    <p class="formInput formSelectBox">
        <select name="state" id="state" class="selectBox"></select>
    </p>
    

    现在怎么办?

    【讨论】:

    • 感谢您的回复,我对此页面中加载 调用然后只获取国家但你的代码是 $('#country').change(function() { print_state1('state' , $(this).val()); });它没有加载任何国家
    • 我从这个站点获取代码bdhacker.wordpress.com/2009/11/21/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2018-01-02
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多