添加javascript onclick到单选按钮INPUT标记

时间:2011-05-10 09:20:29

标签: asp.net vb.net webforms code-behind

我有一个javascript函数,需要将它添加到一些动态创建的单选按钮,从后面的代码中单击。

我试过了 - newRadioSelect.Attributes.Add(“onlick”,“javascript:toggle(this);”)

上面的代码将它添加到单选按钮创建的span标记,任何想法如何在单选按钮的输入标记上获取它?

由于

学家

3 个答案:

答案 0 :(得分:1)

有点像公共汽车,没有多少年龄,然后2出现。

我找到了一种方法来实现它,然后我得到了其他地方的答案,所以我想把它们放在这里。

我的版本(不是最好的)

        Dim newRadioYes As New RadioButton
        newRadioYes.Text = "Yes"
        newRadioYes.ID = "c_" & childID & "_school_selected_0"
        newRadioYes.Attributes.Add("onclick", "javascript:toggle(this, " & childID & ");")
        newRadioYes.Attributes.Add("value", "Yes")
        newRadioYes.GroupName = "c_" & childID & "_school_selected"

        Dim newRadioNo As New RadioButton
        newRadioNo.Text = "No"
        newRadioNo.ID = "c_" & childID & "_school_selected_1"
        newRadioNo.Attributes.Add("onclick", "javascript:toggle(this, " & childID & ");")
        newRadioNo.Attributes.Add("value", "No")
        newRadioNo.GroupName = "c_" & childID & "_school_selected"

更好的版本

        Dim newRadioSelect As New RadioButtonList
        newRadioSelect.RepeatDirection = RepeatDirection.Horizontal
        newRadioSelect.RepeatLayout = RepeatLayout.Flow
        newRadioSelect.Items.Add("Yes")
        newRadioSelect.Items.Add("No")
        newRadioSelect.Items(0).Attributes.Add("onclick", "javascript:toggle(this);")
        newRadioSelect.Items(1).Attributes.Add("onclick", "javascript:toggle(this);")

感谢那些帮助过的人。

答案 1 :(得分:0)

只需使用此javascript函数,您无需将其显式添加到标记中。

http://www.mediaevent.de/javascript/event_listener.html

newRadioSelect.addEventListener("click", myFunction, false); 

function myFunction(event){
    toggle(event.target);
}

答案 2 :(得分:0)

您可以这种方式向动态RadioButton添加属性

rb.Attributes["onClick"] = "javascript:alert('Hi');";