【问题标题】:Setting the value of a dropdown list when a page loads页面加载时设置下拉列表的值
【发布时间】:2009-12-21 16:38:55
【问题描述】:

您好,感谢您的帮助,

我正在尝试在我的页面加载时设置下拉框的值。 现在它没有给我任何错误,但是 dang 下拉框没有设置为我想要的值。

这是我的代码:

<body onLoad="IssuesToReportForm.ReportTo.SelectedValue = '<%=strReportTo%>'">

这部分代码是我的问题吗?

谢谢, 会

【问题讨论】:

  • SelectedValue 是 .NET 的东西...使用 javascript,您只能设置 selectedIndex。也许更多细节?
  • 我相信用我提供的函数循环遍历元素可以解决问题,但还没有尝试过。

标签: drop-down-menu onload selectedvalue


【解决方案1】:
<script type="text/javascript">
  function PreselectMyItem(itemToSelect)
  {

    // Get a reference to the drop-down
    var myDropdownList = document.IssuesToReportForm.ReportTo;

    // Loop through all the items
    for (iLoop = 0; iLoop< myDropdownList.options.length; iLoop++)
    {    
      if (myDropdownList.options[iLoop].value == itemToSelect)
      {
        // Item is found. Set its selected property, and exit the loop
        myDropdownList.options[iLoop].selected = true;
        break;
      }
    }

  }
</script>

假设 IssuesToReportForm 是您的表单名称和您的 ReportTo 下拉列表的名称。

然后&lt;body onLoad="PreselectMyItem('&lt;%=strReportTo%&gt;')"&gt;

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2016-10-13
    相关资源
    最近更新 更多