【问题标题】:3 option select set value from database remove onchange3 选项从数据库中选择设置值删除 onchange
【发布时间】:2015-04-14 04:19:40
【问题描述】:

我有 3 个选择选项,可以根据彼此加载值

我的第一个选择是Continent它在加载第二个选择是Country这个选择将在大陆变化时加载然后最后一个选择是City这将在它工作的国家变化时加载很好

目前由于当我从数据库中加载数据时 select 语句的 onchange 事件,它没有显示正确的数据,因为它显示的是 on change 事件时的数据。

我的问题是如何停止 3 选择选项的 onchange 事件,以便我可以从数据库加载数据我认为停止 onchange 是从数据库加载数据的最佳方式。如果有任何其他选项可以在这些选择上加载数据,我很乐意尝试。

【问题讨论】:

  • 你应该加载Continent从数据库加载页面上选择然后为Continent创建一个更改函数,在这个函数中你应该调用一个ajax并成功函数加载country选择框和像这样加载city
  • 实际上这就是我正在做的我从 ajax 请求加载数据。它工作正常。问题是当我想将数据放入从数据库中选择时,例如我将搜索一个条目组合是Asia,Japan,Tokyo 这不会是结果它仍然会显示来自 document.ready 上的 onchange 事件的数据。我希望我为糟糕的英语感到抱歉。英语不是我的母语

标签: javascript jquery mysql select onchange


【解决方案1】:

要阻止事件在 jQuery 中传播,我们有多个选项,例如 event.stopPropagation()event.preventDefault()

现在在 onchange 函数中,使用 ajax() 方法停止传播并从数据库中加载值。

但我建议在大陆和国家/地区设置 onchange 事件,这样每当它们发生更改时,只需在 onchange 函数中使用 ajax 从数据库中填充相应的子值。无需停止传播。

【讨论】:

    【解决方案2】:

    我为你的州做了一个示例代码。

    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>:: Script Test Page ::</title>
        <script language='javascript'>
        function changeContient()
        {
            var selectItem = document.getElementById('contient').value;
    
            if ( selectItem=='1' )
            {
                // select database, using ajax.
                // add data into country option. under code is example data.
                var country = document.getElementById('country');
                country.options[0] = new Option('1-country1','1-country1');
                country.options[1] = new Option('1-country2','1-country2');
                country.options[2] = new Option('1-country3','1-country3');
            }
            else if ( selectItem=='2' )
            {
                // select database, using ajax.
                // add data into country option. under code is example data.
                var country = document.getElementById('country');
                country.options[0] = new Option('2-country1','2-country1');
                country.options[1] = new Option('2-country2','2-country2');
                country.options[2] = new Option('2-country3','2-country3');
            }
            else if ( selectItem=='3' )
            {
                // select database, using ajax.
                // add data into country option. under code is example data.
                var country = document.getElementById('country');
                country.options[0] = new Option('3-country1','3-country1');
                country.options[1] = new Option('3-country2','3-country2');
                country.options[2] = new Option('3-country3','3-country3');
            }
            else
            {
                return;
            }
        }
    
        function changeCountry()
        {
            // create soruce like changeContient()
        }
        </script>
    </head>
    <html>
        <body>
            Contient :
            <select id='contient' onChange='changeContient();'>
                <option value='1'>Contient1</option>
                <option value='2'>Contient2</option>
                <option value='3'>Contient3</option>
            </select>
            <br/>
            Country :
            <select id='country' onChange='changeCountry();'>
            </select>
            <br/>
        </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 2012-11-13
      • 1970-01-01
      相关资源
      最近更新 更多