2012-09-03 30 views
0

我正在尋找一種方法,當用戶點擊鏈接時,從Asp ListItem中更改選項值。如何從Jquery的鏈接中更改選項值?

比如我有一個天冬氨酸列表項:

<asp:DropDownList ID="DropDownList1" runat="server" class="Preview_link" 
      Height="20px" Width="118px"> 
    <asp:ListItem>Red</asp:ListItem> 
    <asp:ListItem>White</asp:ListItem> 
    <asp:ListItem>Green</asp:ListItem> 
    <asp:ListItem>Blue</asp:ListItem> 
</asp:DropDownList> 

而且我有2個鏈接

<a href="#" title="red" class="preview_link">red</a> 
<a href="#" title="white">white</a> 

當用戶點擊紅色的選項將切換爲紅色和白色將切換爲白色。我正在使用下面的代碼,但它不工作。

我該如何去改變這個jQuery的工作與Asp ListItem而不是HTML下拉?

jQuery("a.preview_link").click(function() { 
    var title = jQuery(this).attr("title"); 
    jQuery(this).parent('p').find("select option").filter(function() { 
     return $(this).text() == title; 
     }).attr('selected', 'selected'); }); 

回答

0

如果你的選擇是好的 - 這應該工作:

jQuery("a.preview_link").click(function() { 
    var title = jQuery(this).attr("title"); 
    jQuery(this).parent('p').find("select option").filter(function() { 
     return $(this).text().toLowerCase() == title; 
    }).attr('selected', 'selected'); 
}); 

但是,這個功能看起來真的creazy。您可以通過 jQuery(this).parent('p').find("select").val(title)選擇一個選項。這裏只有一件事 - 我不確定是否區分大小寫,但假設案例很重要。

我該如何去改變這個jQuery的工作與Asp ListItem而不是HTML下拉菜單?

目前尚不清楚你想要什麼。 asp:ListItem是一個HTML下拉菜單,如果你正在瀏覽器中查看。

0

更簡單,你可以這樣做:

$("a.preview_link").click(function() { 
    var title = $(this).attr("title"); 
    $('#DropDownList1 option[value=title]').attr("selected", true); 
    }); 
+0

沒有#DropDownList1。在asp.net中它將被翻譯成類似'#ctr01_ContentContainer_DropDownList1'的東西# –

+0

但是這可能與$(「select.Preview_link ...」)一起工作 –

+0

爲什麼id選擇器不能使用asp? – fatiDev