开发者

Populate second dropdown using jQuery & Ajax

开发者 https://www.devze.com 2023-04-12 22:46 出处:网络
I am trying to populate a second dropdown list based on the value of the first dropdown from an external html file which is only filled with the options.

I am trying to populate a second dropdown list based on the value of the first dropdown from an external html file which is only filled with the options.

Example of external file:

<option value="Bedfordshire">Bedfordshire</option开发者_JS百科>
<option value="Berkshire">Berkshire</option>
<option value="Buckinghamshire">Buckinghamshire</option>

Example of first dropdown values:

 <select>
 <option value="GB">UNITED KINGDOM</option> //option to load drop-GB.html
 <option value="US">UNITED STATES</option> //option to load drop-US.html
</select>

It all works fine in FF/Safari/Chrome but not at all in IE or iPad?

var $shipcountry = $('#ShippingCountry');
 $ShippingStateSelect = $('#ShippingStateSelect');
   $ShippingStateSelect.load('drop-GB.html'); //pre load default list

  $shipcountry.change(function () {
    var countryName = $shipcountry.val();

        $.ajax({
            type: 'GET',
            url: 'drop-' + countryName + '.html',
            success: function (msg) {       
                $ShippingStateSelect.load('drop-' + countryName + '.html');
        //fire other events on page
            },
            error: function (msg) {
               $ShippingStateSelect.hide();
        //show error message here
            },

        }); 

  });


You are not sorting you incoming HTML, and since it isnt "pure" elements IE fails while Firefox/Chrome etc tries to fix it.

Your drop-US.html contains HTML structure like

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- BC_OBNW -->
<head>
<title>drop-US</title>
<link href="/StyleSheets/ModuleStyleSheets.css" type="text/css" rel="StyleSheet" />
<script type="text/javascript">var jslang='EN';</script>
</head>

Which it then tries to insert into the selectbox.

So you should either filter it out in the ajax request, or remove it in the source. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号