2017-07-17 111 views
0

我使用的是asp.net web表單,並在某些選擇更改下拉菜單時觸發了一個事件。事件不在後面的代碼中,而是在事件觸發時執行的JavaScript方法。我需要根據下拉列表中更改的值更改某個標籤的文字。asp.net javascript - 更改下拉選擇更改標籤文本

我是javascript的新手,無法找到訪問標籤的「文本」屬性的方法。 有人可以幫忙嗎?

<script type="text/javascript"> 
    function myMethod(sender, args) { 

     ............ 
    } 
</script> 

回答

0

這裏有一個簡單的例子。要記住的最重要的事情是,aspnet可以重命名生成的html中元素的ID。所以總是用ClientID

<asp:DropDownList ID="DropDownList1" runat="server"> 
    <asp:ListItem Text="Item 1" Value="1"></asp:ListItem> 
    <asp:ListItem Text="Item 2" Value="2"></asp:ListItem> 
    <asp:ListItem Text="Item 3" Value="3"></asp:ListItem> 
</asp:DropDownList> 

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 


<script type="text/javascript"> 
    $('#<%= DropDownList1.ClientID %>').change(function() { 
     $('#<%= Label1.ClientID %>').text($(this).val()); 
    }); 
</script> 
0

在html中,您可以通過使用onchange屬性放置事件來調用事件。您可以使用此關鍵字在JavaScript中獲得價值。例如我在這裏做一個小演示https://jsfiddle.net/3nfvy6ke/5/

<select onchange="javascript:test(this)"> 
<option value="1">1</option> 
<option value="2" selected>2</option> 
</select> 
<script> 
function test(ele){ 
debugger; 
document.write(ele.value); 
} 
</script>