将控制作为参数传递给 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7566041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing control as parameter to javascript function
提问by k80sg
I am trying to pass a control's id to a javascript function that adds the value of it(the control which is a textbox) to a listbox but apparently I am not getting it right, can someone please correct me.
我正在尝试将控件的 id 传递给一个 javascript 函数,该函数将它的值(文本框控件)添加到列表框,但显然我没有做对,有人可以纠正我。
Thanks.
谢谢。
<input type="button" ID="btnAddtoLstBox" value="" title="Add this to the list" onclick="javascript:addToList(document.getElementById(btnAddtoLstBox));"
class="ui-icon ui-icon-refresh ui-corner-all" style="width: 20px; height: 20px; background-position: -64px 80px" />
// scripts to add list items
function addToList(varTxtBox) {
// get the list box
var lb = document.getElementById("uilstMemTypeTier");
// get the text to add
var toAdd = varTxtBox.value;
if (toAdd == "") return false;
// look for the delimiter string. if found, alert and do nothing
if (toAdd.indexOf(delim) != -1) {
alert("The value to add to the list cannot contain the text \"" + delim + "\" as it is used as the delimiter string.");
return false;
}
// check if the value is already in the list box
for (i = 0; i < lb.length; i++) {
if (toAdd == lb.options[i].value) {
alert("The text you tried to add is already in the list box.");
return false;
}
}
// add it to the hidden field
document.getElementById("<%=uihdnlistBasedFieldsListItems.ClientID%>").value += toAdd + delim;
// create an option and add it to the end of the listbox
lb.options[lb.length] = new Option(toAdd, toAdd);
// clear the textfield and focus it
varTxtBox.value = "";
varTxtBox.focus();
}
回答by hungryMind
Change onclick="javascript:addToList(document.getElementById(btnAddtoLstBox));"
to onclick="addToList(document.getElementById('btnAddtoLstBox'));"
or onclick="addToList(this);"
更改onclick="javascript:addToList(document.getElementById(btnAddtoLstBox));"
到onclick="addToList(document.getElementById('btnAddtoLstBox'));"
或onclick="addToList(this);"
回答by Samich
If you are using control event handler you can provide this
and it be control:
如果您正在使用控制事件处理程序,您可以提供this
它是控制:
onclick="addToList(this)"
回答by ShaileshDev
You can also do it in following way-
您也可以通过以下方式进行 -
<body>
<form id="form1" runat="server">
<div id="div1" style="height:100px; width:192px; background-color:red;">
</div>
<br />
<div id="div2" style="height:100px; width:192px; background-color:green; display:block">
</div>
<br />
<asp:Button runat="server" Text="Change color" id="btnColor" OnClientClick="javascript:return changeColor();"/>
<asp:Button Text="Hide 1st" runat="server" ID="btnHide1st" OnClientClick="javascript:return hideDiv('div1');"/>
<asp:Button Text="Hide 2nd" runat="server" id="btnHide2nd" OnClientClick="javascript:return hideDiv('div2');"/>
</form>
Hope this may have helped you.
希望这可能对您有所帮助。