簡體   English   中英

asp.net JavaScript-在更改下拉選擇時更改標簽文本

[英]asp.net javascript - change the label text on changing drop down selection

我正在使用asp.net Web表單,並且某些下拉選擇發生更改時觸發了一個事件。 事件不在后面的代碼中,而是事件觸發時執行的javascript方法。 我需要根據下拉列表中更改的值來更改某些標簽的文本。

我是javascript的新手,找不到找到標簽的“文本”屬性的方法。 有人可以幫忙嗎?

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

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

這是一個簡單的例子。 要記住的最重要的一點是,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>

在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM