开发者

Chrome Drop-Down List Problem

开发者 https://www.devze.com 2023-02-08 18:58 出处:网络
I have a problem with Chrome. I have a drop-down list. Except for Chrome, it works well. However with chrome it adds an empty element at first.

I have a problem with Chrome. I have a drop-down list. Except for Chrome, it works well. However with chrome it adds an empty element at first.

HTML Part:

<head>   
<script type="text/javascript">       
  $(document).ready(function() {
  townDistrictObject.bind({cityId:1});
});
</script>
</head>
    <body>               
        <div class="left marginleft15">
            <p>Town</p>
            <p>
                <select name="town1" id="townId" style="width: 152px;">
                    <option selected="selected" value="999999999">Whole Istanbul</option>
                    <option value="999999998">Anatolian Part</option>
                </select>
            </p>
        </div>
        <div class="left marginleft15">
            <p>District</p>
            <p>
                <select id="districtId" name="districtid1" style="width: 174px;" >
                    <option selected="selected" value="0">Whole Districts</option>
                </select>
            </p>
        </div>
    </body>

There is something like that at script side:

var townDistrictObject = {
   defaults : {
      cityId :1, 
      townElementId : "townId",
      districtElementId : "districtId",
      allDistricts: true
},

bind : function(options) {
  $.extend(this.defaults, options);
  var that = this;
  var opts = this.defaults;
  var townElement = $('#' + opts.townElementId);
  townElement.val(开发者_如何学Python0);           
 }
};

Explanation of problem:

Firstly, there is an empty element at top of list.(It shouldn't be!)

Chrome Drop-Down List Problem

Secondly, I will click one of them.("Whole Istanbul" for my example.)

Chrome Drop-Down List Problem

Lastly, I check the list and that element at top disappears. Everything is OK after that time.

Chrome Drop-Down List Problem

PS: I checked the code and I think that the problem is related to script side. Thanks for your help.


Could it be that you are doing:

townElement.val(0); 

You are setting the dropdown value to zero, when there is no option with this value. Instead of zero, should you be using the value from the defaults?


The problem is setting with wrong id at this line:

townElement.val(0);

The answer with Jquery replacing it with:

townElement.get(0).selectedIndex = 0;
0

精彩评论

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