如何将值传递给 aspx 页面 onClientClick 中的 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13496173/
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
How to pass a value into a javascript function within an aspx page onClientClick
提问by anmarti
I want to check the textbox value before postback occurs. I set the onClientClickvalue to my function but I don't know how to pass the data to check, in this case I want to check the txt1 entered text.
我想在回发发生之前检查文本框值。我将onClientClick值设置为我的函数,但我不知道如何传递数据进行检查,在这种情况下我想检查 txt1 输入的文本。
<asp:textbox id="txt1" runat="server />
<asp:LinkButton ID="LinkButton1"
runat="server"
Text="Save"
onclick="Save"
OnClientClick="javascript:check(WANT TO PUT TEXTBOX VALUE HERE);"
/>
My javascript:
我的javascript:
function check(txt) {
var pattern = /^[0-9]{1,11}(,[0-9]{0,2})?$/;
if (!pattern.test(txt)) {
return false;
}
else {
return true;
}
}
The issue is that this check function is used along the keypress event of the txt1 so I cant use:
问题是这个检查函数是在 txt1 的按键事件中使用的,所以我不能使用:
function check(){
var txt=$('#txt1').val();
}
The whole JS code:
整个JS代码:
$('#txt1').keypress(function (e) {
var key = String.fromCharCode(e.which);
var txt = $(this).val() + key;
if (check(txt)) {
// do sth
} else {
e.preventDefault();
// do sth
}
});
How can I tell the OnclientCLick function to get the txt1 value?
如何告诉 OnclientCLick 函数获取 txt1 值?
Thank you.
谢谢你。
采纳答案by Samuel Caillerie
Something like that :
类似的东西:
<asp:textbox id="txt1" runat="server />
<asp:LinkButton ID="LinkButton1"
runat="server"
Text="Save"
onclick="Save"
OnClientClick="check(document.getElementById('<%=txt1.ClientID%>').value;)"
/>
Edit:
Other methods :
编辑:
其他方法:
You can add it on server side :
LinkButton1.OnClientClick="check(document.getElementById('" + txt1.ClientID + "').value;)";If you want to remain in the aspx page, you have to put a name of a javascript function in the OnClientClick and implement the javascript function in a script tag :
<asp:LinkButton ID="LinkButton1" runat="server" Text="Save" OnClientClick="validate();" /> <script type="text/javascript"> function validate() { alert(document.getElementById('<%=txt1.ClientID%>').value); return false; } </script>
您可以在服务器端添加它:
LinkButton1.OnClientClick="check(document.getElementById('" + txt1.ClientID + "').value;)";如果你想留在 aspx 页面,你必须在 OnClientClick 中放置一个 javascript 函数的名称,并在脚本标签中实现 javascript 函数:
<asp:LinkButton ID="LinkButton1" runat="server" Text="Save" OnClientClick="validate();" /> <script type="text/javascript"> function validate() { alert(document.getElementById('<%=txt1.ClientID%>').value); return false; } </script>
回答by elucid8
You have several approaches.
你有几种方法。
First Approach:
第一种方法:
function sayHello(obj){
alert(obj.id + " says 'Hello!'");
};
<asp:Button runat="server" ID="btnApproach1" OnClientClick="sayHello(this)"
Text="Say Hello" />
This will pass the Button control (actually, just an <inputelement) to the function and you can then work on it however you please.
这会将 Button 控件(实际上,只是一个<input元素)传递给该函数,然后您可以随意处理它。
Second Approach:
第二种方法:
function checkValue(e) {
//If it has a target, use it. Otherwise, use srcElement (for IE)
var ctrl = e.target || e.srcElement;
//Do whatever you need to with the text, no button necessary.
};
function bindEvent(ctrl, event, handler, propagates) {
if (ctrl.addEventListener) {
ctrl.addEventListener(event, handler, propagates);
}
else {
ctrl.attachEvent("on" + event, handler);
}
};
window.onload = function () {
var myCtrl = document.getElementById('<%=txtToValidate.ClientID%>');
bindEvent(myCtrl, "keydown", checkValue, false);
};
<asp:Button runat="server" ID="btnApproach2" Text="Say Hello" />
Here, you are binding the events to the controls behind the scenes (preferable) and removing that logic from your presentation layer (your .aspx).
在这里,您将事件绑定到幕后的控件(最好)并从您的表示层(您的 .aspx)中删除该逻辑。
Both approaches will work, but I'd suggest going with option two for separation of concerns.
两种方法都有效,但我建议使用选项二来分离关注点。
回答by Ravia
You simply have to add single quote to both end of your string passes to check function as shown below.
您只需在字符串传递的两端添加单引号即可检查函数,如下所示。
javascript:check('WANT TO PUT TEXTBOX VALUE HERE');

