设置的jQuery一个文本框的值 [英] Setting a textbox value by JQuery

查看:110
本文介绍了设置的jQuery一个文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Ajax岗位设置文本框控制值。

I am setting a TextBox controls value via an ajax post.

$('#txtSite').val(msg.d.SiteName);

这是工作和文本框的值正确改变。但是,当我来到张贴信息添加到数据库中, txtSite.Text 值是空的!

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!!

任何想法?难道我要疯了?

Any ideas? Am I going mad?

code来填充文本框

$.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保存到服务器(所有的连接等是正确的工作)。这code是在ASP按钮点击事件:

Code to save to the server (all connectivity etc. is correct and working). This code is in an ASP button click event:

SqlCommand cmd = new SqlCommand("INSERT INTO [Sites] ([SiteName]) VALUES ('" + txtSite.Text + "')", conn);
cmd.ExecuteNonQuery();

文本框的定义,像这样:

<asp:TextBox ID="txtSite" runat="server" AutoComplete="off" TabIndex="4" MaxLength="50" Style="width: 230px" Enabled="false" CssClass="FormDisabledTextWithSmallHeight" />

我也试图改变我的JQuery使用普通的JavaScript来代替,这样做的:

I have also tried changing my JQuery to use plain Javascript instead and doing this:

document.getElementById("txtSite").value = msg.d.SiteName;

不过我返回空值。

Still returns me an empty value.

推荐答案

你有你的文本框设置为启用=false的这使得在浏览器中使用禁用=禁用 <一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontrol.disabled.aspx\">Disabled表单输入未提交。

You have your textbox set to Enabled="false" which renders in the browser with a disabled="disabled". Disabled form inputs are not submitted.

解决方法是,使使文本框和只读:

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

或者使用与 =服务器,像&LT设置了不同的元素; ASP:HiddenField /&GT; 和更新两个文本框,并与您的AJAX调用备用元素:

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:

success: function(msg) {
    if (msg.d != null) {
        $('#txtSite').val(msg.d.SiteName);
        $('#hiddenInput').val(msg.d.SiteName);
    } else {
            $('#txtSite').val('');
    }
}

这篇关于设置的jQuery一个文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆