在javascript函数中获取按钮名

时间:2021-11-24 07:03:50

I have a html button with runat=server tag, whoose Name has been set dynamically during run time.

我有一个带有runat=server标记的html按钮,在运行时动态地设置了whoose名称。

    protected void Page_Load(object sender, EventArgs e)
    {
        btnShowHideCols.Name = "some text here";
    }

the button HTML code is like below.

按钮HTML代码如下所示。

    <input id="btnShowHideCols" type="button" value="+/-" runat="server" onclick="return btnShowHideCols_onclick()" />

How Do I get the button name form inside the javascript function? can I get the sender of the event there?

如何在javascript函数中获取按钮名称表单?我能找到事件的发件人吗?

2 个解决方案

#1


1  

You may do this:

你可以这样做:

    <input id="btnShowHideCols" type="button" 
value="+/-" runat="server" onclick="return btnShowHideCols_onclick(this)" />

and than:

比:

function btnShowHideCols_onclick(el) {
   el.getAttribute("name");
}

While writing something like onclick="" or onchange="" or any other event you can refer to current object with this.

当您编写onclick=""或onchange="或任何其他事件时,您可以使用这个引用当前对象。

Also, note that name will not be the same like in your code. It will be changed to something like ctl00$content$test, just the same as ID

另外,请注意,在您的代码中名称将不相同。它将被更改为ctl00$content$test,与ID相同

#2


0  

Im quiet confused about what your trying to do..

我完全不明白你想做什么。

Is this it:

是这样的:

document.getElementByid("btnShowHideCols").getAttribute("name")

^Will return the name

^将返回这个名字

Then use this to send it to server.. Or am i wrong here?

然后用这个发送到服务器。还是我错了?

Hope this helps!

希望这可以帮助!

#1


1  

You may do this:

你可以这样做:

    <input id="btnShowHideCols" type="button" 
value="+/-" runat="server" onclick="return btnShowHideCols_onclick(this)" />

and than:

比:

function btnShowHideCols_onclick(el) {
   el.getAttribute("name");
}

While writing something like onclick="" or onchange="" or any other event you can refer to current object with this.

当您编写onclick=""或onchange="或任何其他事件时,您可以使用这个引用当前对象。

Also, note that name will not be the same like in your code. It will be changed to something like ctl00$content$test, just the same as ID

另外,请注意,在您的代码中名称将不相同。它将被更改为ctl00$content$test,与ID相同

#2


0  

Im quiet confused about what your trying to do..

我完全不明白你想做什么。

Is this it:

是这样的:

document.getElementByid("btnShowHideCols").getAttribute("name")

^Will return the name

^将返回这个名字

Then use this to send it to server.. Or am i wrong here?

然后用这个发送到服务器。还是我错了?

Hope this helps!

希望这可以帮助!