通过 JQuery 设置文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14484168/
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
Setting a textbox value by JQuery
提问by Neil Knight
I am setting a TextBox
controls value via an ajax post.
我正在TextBox
通过 ajax 帖子设置控件值。
$('#txtSite').val(msg.d.SiteName);
This is working and the value of the TextBox
is altered correctly. But, when I come to posting the information to the database, the txtSite.Text
value is empty!!
这是有效的,并且 的值TextBox
已正确更改。但是,当我将信息发布到数据库时,该txtSite.Text
值为空!!
Any ideas? Am I going mad?
有任何想法吗?我要疯了吗?
Code to populate the TextBox
:
代码填充TextBox
:
$.ajax({
type:"POST",
url: "MyPage.aspx/ValidateSite",
data: "{ siteID: '" + $('#txtSiteID').val() + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if (msg.d != null) {
$('#txtSite').val(msg.d.SiteName); // It's definitely doing this
}
else {
$('#txtSite').val('');
}
},
error: function(msg) {
}
});
Code to save to the server (all connectivity etc. is correct and working). This code is in an ASP button click event:
保存到服务器的代码(所有连接等都正确且工作正常)。此代码位于 ASP 按钮单击事件中:
SqlCommand cmd = new SqlCommand("INSERT INTO [Sites] ([SiteName]) VALUES ('" + txtSite.Text + "')", conn);
cmd.ExecuteNonQuery();
The TextBox
is defined like so:
的TextBox
定义如下所示:
<asp:TextBox ID="txtSite" runat="server" AutoComplete="off" TabIndex="4" MaxLength="50" Style="width: 230px" Enabled="false" CssClass="FormDisabledTextWithSmallHeight" />
I have also tried changing my JQuery to use plain Javascript instead and doing this:
我也尝试过将我的 JQuery 改为使用纯 Javascript 并执行以下操作:
document.getElementById("txtSite").value = msg.d.SiteName;
Still returns me an empty value.
仍然返回一个空值。
回答by pete
You have your textbox set to Enabled="false"
which renders in the browser with a disabled="disabled"
. Disabled form inputs are not submitted.
您已将文本框设置为Enabled="false"
在浏览器中以disabled="disabled"
. 不提交禁用的表单输入。
The solution is either to make the textbox enabled and read-only:
解决方案是使文本框启用和只读:
txtSite.Enabled = true;
txtSite.Attributes.Add("readonly", "readonly"); //on prerender event or somewhere else
or to use a different element set with runat="server"
, like a <asp:HiddenField />
and update both the textbox and the alternate element with your AJAX call:
或者使用不同的元素集runat="server"
,例如 a<asp:HiddenField />
并使用 AJAX 调用更新文本框和替代元素:
success: function(msg) {
if (msg.d != null) {
$('#txtSite').val(msg.d.SiteName);
$('#hiddenInput').val(msg.d.SiteName);
} else {
$('#txtSite').val('');
}
}
回答by Rana
Yes, I definitely think, your text box should have a name attribute in html code, check for it. Without a 'name' attribute browser won't post this input box's data to server and thus you won't get it in server side c# code. Hope, this make sense..
是的,我绝对认为,您的文本框应该在 html 代码中具有 name 属性,请检查它。如果没有“名称”属性,浏览器不会将此输入框的数据发布到服务器,因此您不会在服务器端 C# 代码中获得它。希望,这是有道理的..