簡體   English   中英

單選按鈕列表選定索引動態變化時如何調用JS函數

[英]how to call JS function when radiobutton list selected index changed dynamically

我有幾個單選按鈕列表,我需要根據其他單選按鈕選擇來更改幾個列表的選定索引,為此,我編寫了如下內容,並且我在onclick事件上調用了一些JS函數,在OnSelectedIndexChanged事件上使用了codebehind方法精細..

function test(sender) {
            if (sender[0].checked)
            {
                var controlid = document.getElementById("RadioButtonList1");
                controlid[0].checked = true;
           }

<asp:RadioButtonList ID="RadioButtonList1" runat="server"
                RepeatDirection="Horizontal" 
                OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" onclick="validate()">
                <asp:ListItem >YES</asp:ListItem>
                <asp:ListItem >NO</asp:ListItem>
            </asp:RadioButtonList>
            <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem onclick="test()">YES</asp:ListItem>
                <asp:ListItem onclick="test()">NO</asp:ListItem>

            </asp:RadioButtonList>

問題:如果我在RadioButtonList2中選擇YES,則RadioButtonList1的值也設置為YES,但是在這種情況下,不會調用從RadioButtonList1調用的函數(validate(),RadioButtonList1_SelectedIndexChanged)。

您能告訴我如何解決此問題嗎?.jquery或JS中是否有任何功能可動態檢測到索引變化以調用方法。

如果我理解正確,則希望基於所選的單選按鈕更改選擇框的所選值。

為什么不將單選按鈕與jquery onchange事件綁定,在該函數中,您可以決定如何使用選擇框所選值。

如果使單選按鈕的值與每個相應單選按鈕的選擇框的值相同,則可以執行以下操作:

    jQuery("#radio_btns").on("change", function(){
        jQuery("#select_element").val(jQuery(this).val())
    })

其中#radio_btns是單選按鈕的集合,而#select_element是您要更新的選擇框。

希望這可以幫助!

我會看一下jquery文檔,它們通常有大量的代碼片段作為示例。 檢出“開”功能,以及可能的觸發功能。 您基本上只需要更改事件。 希望這可以幫助。

api.jquery.com

暫無
暫無

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

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