简体   繁体   English

选择和取消选择在IE9中不起作用?

[英]Select and Deselect not working in IE9?

I have a dropdown <select> element to select states from the list of 50 states, I select the 1st value, save it, and show the value in DOM. 我有一个下拉<select>元素,可从50个状态的列表中选择状态,我选择第一个值,将其保存,并在DOM中显示该值。 I changed and select to the 5th value, saving it shows the value updates in DOM. 我更改并选择了第5个值,保存后显示了DOM中的值更新。 Now back i am selecting the 2nd Value, and saving it. 现在返回,我选择第二个值并保存。 It's not saving the value in DOM and it's showing the previous selected 5th value. 它没有将值保存在DOM中,而是显示了先前选择的第5个值。 I Checked with different values, and found that, after selecting any higher index value, selecting back, lower values are not affecting in DOM, and hence i am not getting the correct values in POST. 我检查了不同的值,发现选择较高的索引值后再选择较低的值不会影响DOM,因此在POST中无法获取正确的值。

This is the function i am calling on change . 这就是我要求进行change的功能。

function updateDOM(inputField) {
    var elementId = inputField;
    if (inputField.type == "select-one") {
       var prev_select = inputField.selectedIndex;
       $('#'+inputField.id+' option').each(
          function() {
             $(this).removeAttr('selected');
          }
       );
       document.getElementById(elementId.id).selectedIndex = prev_select;
       if (browserVersion == 9) {
          document.getElementById(elementId.id)
             .options[prev_select].setAttribute("selected","selected");
       }
       else {
          document.getElementById(elementId.id)
             .options[prev_select].setAttribute("selected","selected");
       }
       document.getElementById(elementId.id).value
          = document.getElementById(elementId.id).options[prev_select].value;
}

The HTML HTML

   <select id="abc"  name="abc"  onchange="javascript:updateDOM(this);" class="required" >
      <option name="" value="" title="null" selected ></option>
      <option name="AK" value="Alaska" title="null"  >Alaska</option>
      <option name="AL" value="Alabama" title="null"  >Alabama</option>
      <option name="AR" value="Arkansas" title="null"  >Arkansas</option>
      <option name="AZ" value="Arizona" title="null"  >Arizona</option>
   </select>

First of all, why don't you use ":selected" selector of jQuery, which you are using anyway? 首先,为什么不使用jQuery的“:selected”选择器呢? Also, why are you using jQuery only once? 另外,为什么只使用一次jQuery? I would recommend doing it in jQuery-style (sorry, I'm not quite sure what you are trying to do exactly in your code): 我建议以jQuery风格进行此操作(对不起,我不太确定您要在代码中尝试执行的操作):

http://jsfiddle.net/msLXt/1/ http://jsfiddle.net/msLXt/1/

PS What is the difference between these conditions? PS这些条件之间有什么区别?

   if (browserVersion == 9) {
      document.getElementById(elementId.id)
         .options[prev_select].setAttribute("selected","selected");
   }
   else {
      document.getElementById(elementId.id)
         .options[prev_select].setAttribute("selected","selected");
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM