传递JQuery的变量code在asp.net背后 [英] Passing JQuery variable to code behind in asp.net
问题描述
我一直在尝试了一段时间,以获得jQuery函数传递到后面,这样我可以通过一个SQL语句,将其输入到数据库中的code中的变量。但是,我似乎从来没有得到变量传递。或者如果它是过客,我似乎无法得到它的显示,以确保它已经过去了。任何人都可以看到有什么不对我的脚本?
<脚本类型=文/ JavaScript的>
$(函数(){
VAR评论= $(#注释),
allFields = $([])加(评论)。
$('#<%= hidden.ClientID%GT;')。VAL(注解);
$(#对话框的形式)。对话框({
的AutoOpen:假的,
高度:300,
宽度:350,
模式:真实,
纽扣: {
添加评论:功能(){
VAR bValid = TRUE;
allFields.removeClass(UI状态错误);
如果(bValid){
$(#评论TBODY)追加(< TR>中。+
&所述; TD>中+ comment.val()+&下; / TD>中+
&所述; TD>中+&下;%=的currentUser%gt;中+&下; / TD>中+
&所述; / TR>中); $(本).dialog(亲密);
//
}
},
取消:功能(){
$(本).dialog(亲密);
}
},
关闭:函数(){
allFields.val()(UI态错误)removeClass移除。
}
}); $(#NewComment)
.button()
。点击(函数(){
$(#对话框的形式)对话框(开放)。 });
});
< / SCRIPT>
这是我隐藏的输入字段:
<输入ID =隐藏TYPE =隐藏=服务器/>
我会建议使用一个实际的Asp.Net HiddenField
控制是这样的:
标记
< DIV>
< ASP:HiddenField ID =隐藏=服务器/>
< ASP:按钮的ID =Button1的=服务器的onclick =的button1_Click文本=按钮/>
< / DIV>
code背后
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
串藏= this.hidden.Value;
的Response.Write(隐藏);
}
的JavaScript
<脚本>
$(文件)。就绪(函数(){
$(#<%= hidden.ClientID%GT;)。VAL(你好,世界!);
});
< / SCRIPT>
输出:你好,世界
I have been trying for a while to get the variable passed from the JQuery function to the code behind so that I can enter it into a database via an SQL statement. However, I never seem to get the variable passed. Or if it is passing, I can't seem to get it to display to make sure it has been passed. Can anyone see what's wrong with my script?
<script type="text/javascript">
$(function () {
var comment = $("#comment"),
allFields = $([]).add(comment);
$('#<%= hidden.ClientID %>').val(comment);
$("#dialog-form").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add Comment": function () {
var bValid = true;
allFields.removeClass("ui-state-error");
if (bValid) {
$("#comments tbody").append("<tr>" +
"<td>" + comment.val() + "</td>" +
"<td>" + "<%=currentUser%>" + "</td>" +
"</tr>");
$(this).dialog("close");
//
}
},
Cancel: function () {
$(this).dialog("close");
}
},
close: function () {
allFields.val("").removeClass("ui-state-error");
}
});
$("#NewComment")
.button()
.click(function () {
$("#dialog-form").dialog("open");
});
});
</script>
And here is my hidden input field:
<input id="hidden" type="hidden" runat="server" />
I would suggest to use an actual Asp.Net HiddenField
control like this:
Markup:
<div>
<asp:HiddenField ID="hidden" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
Code Behind:
protected void Button1_Click(object sender, EventArgs e)
{
string hidden = this.hidden.Value;
Response.Write(hidden);
}
Javascript:
<script>
$(document).ready(function () {
$("#<%=hidden.ClientID %>").val("Hello, World!");
});
</script>
Output: Hello, World!
这篇关于传递JQuery的变量code在asp.net背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!