javascript - 将控制作为参数传递给 javascript 函数

标签 javascript asp.net vb.net

我正在尝试将控件的 id 传递给 JavaScript 函数,该函数将其值(文本框控件)添加到列表框,但显然我没有得到正确的结果,有人可以纠正我吗?

谢谢。

<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();
            }

最佳答案

onclick="javascript:addToList(document.getElementById(btnAddtoLstBox));" 更改为 onclick="addToList(document.getElementById('btnAddtoLstBox'));"onclick="addToList(this);"

关于javascript - 将控制作为参数传递给 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7566041/

相关文章:

c# - 在 c# 中使用/不使用正则表达式清除不需要的十六进制字符

asp.net - 已编译的 XSLT 如何在 IIS 上工作?

vb.net - VB.NET读取注册表

wpf - 有没有一种简单的方法可以绑定(bind)到 WPF 中的父行?

asp.net - System.Web.Security.FormsAuthentication.Encrypt 返回 null

javascript - 更改嵌套状态的所有值?

javascript - 是否可以将 d3.js 与 gatsby.js 框架一起使用?

javascript - 更改自定义函数的内容

EditorFor 模板的 VB.NET 语法?

javascript - 不寻常的响应表功能