2009-02-26 29 views
0

我試圖從使用JavaScript的SQL Server Reporting Services創建的頁面訪問下拉框。我通常只是getElementById,但下拉元素的id和name屬性是由服務器生成的,所以我不想用它來引用元素。即,如果頁面設計將來發生變化,它將命名該元素_ct105或_ct107從SQL Server Reporting Services獲取字段頁面

我在頁面上指定的唯一東西是標籤「Business Period」。我正在考慮使用xpath引用該跨度,然後使用相對位置到下一個select元素,但我無法弄清楚如何做到這一點。

理想情況下,我可以指定選擇元素本身的ID或名稱(或任何其他屬性)。這可能嗎?

我將在頁面上使用jQuery,所以如果任何人都可以想出一種很好的方式來找到它,那就太棒了。

<tr IsParameterRow="true"> 
<td class="ParamLabelCell"> 
    <span>Business Period</span> 
</td> 
<td class="ParamEntryCell" style="padding-right:0px;"> 
    <div> 
     <select name="ctl140$ctl00$ctl03$ddValue" id="ctl140_ctl00_ctl03_ddValue"> <!-- This is the select box I want --> 
      <option value="0">&lt;Select&nbsp;a&nbsp;Value&gt;</option> 
      <option value="1">Customer</option> 
      <option value="2">Previous&nbsp;Week</option> 
      <option value="3">Current&nbsp;Week</option> 
      <option value="4">Next&nbsp;Week</option> 
     </select> 
    </div> 
</td> 

回答

2

如果您已經在使用jQuery,那麼XPath會過度殺傷。我在jQuery中看到了幾種不同的方法。 jQuery使用CSS選擇器,所以這就是如何將這些所有的工作:

/* this attaches to selectboxes, which are in a div, 
* which is in a [TD] with class ParamEntryCell 
*/ 
$("td.ParamEntryCell div select"); 
/* selects a selectbox whose id attribute starts with "ctl" 
    * in an element with class ParamEntryCell 
    */ 
$(".ParamEntryCell select[name^='ctl']") 
$("td.ParamEntryCell div select"); 
/* selects a selectbox whose id attribute starts with "ctl" 
    * in an element with class ParamEntryCell 
    */ 
$(".ParamEntryCell select[id^='ctl']") 

然後使用這些您只需使用jQuery的建立方法之一:

$(".ParamEntryCell select[id^='ctl']").change(function(){ 
    var val = $(this).val(); 
    alert(val); 
}) 
+0

你怎麼得到這個注入報告? – 2009-04-25 19:02:31