如何使用javascript在asp:textbox中显示值

如何使用javascript在asp:textbox中显示值,javascript,asp.net,Javascript,Asp.net,想要使用JavaScript在asp:textbox中显示数据。当我使用HTML文本框时,该值显示在文本框上。但在使用asp:Textbox时,不会显示数据 function SearchDealer() { $.ajax( { async: false, type: "POST", url: "DealerDetails.aspx/GetDealerDetails", data: "{DlrId:'" + deal

想要使用JavaScript在
asp:textbox
中显示数据。当我使用HTML文本框时,该值显示在文本框上。但在使用
asp:Textbox
时,不会显示数据

function SearchDealer()
{
    $.ajax(
    {
        async: false,
        type: "POST",
        url: "DealerDetails.aspx/GetDealerDetails",
        data: "{DlrId:'" + dealerID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data)
        {  
            //This is a html text box so value is dsplayed.
            document.getElementById('txt_sapcode').value = data.d.sapcode;

            //No values displayed in this asp:textBox
            document.getElementById('tex_dealername').value = data.d.DealerName;
            document.getElementById('txt_addr1').value = data.d.Add1;
        },
        error: function ()
        {
            alert("Failed to update details.");
        }
    });
    srchflg = true;
}

将CssClass属性添加到asp:Textbox并使用jquery($(“.class”))获取它。

将CssClass属性添加到asp:Textbox并使用jquery($(“.class”))获取它。

asp:Textbox
控件生成唯一ID,因此:

<asp:Textbox ID="myTextBox" runat="server" Text="Hello, World" />
然后需要将函数定义声明为:

function SearchDealer(dealerName) {
    // Do something with dealerName
}

asp:Textbox
控件生成唯一的ID,因此:

<asp:Textbox ID="myTextBox" runat="server" Text="Hello, World" />
然后需要将函数定义声明为:

function SearchDealer(dealerName) {
    // Do something with dealerName
}

使用ASP.NET控件,为您提供在服务器端生成的唯一ID,您的ID如下所示
ctl00\u contentPlaceHolder\u tex\u dealername

客户端代码:

$("input[id$='tex_dealername']").val(data.d.DealerName);  //option 1
$('<%= tex_dealername.ClientID %>').val(data.d.DealerName); //option 2 

使用ASP.NET控件,为您提供在服务器端生成的唯一ID,您的ID如下所示
ctl00\u contentPlaceHolder\u tex\u dealername

客户端代码:

$("input[id$='tex_dealername']").val(data.d.DealerName);  //option 1
$('<%= tex_dealername.ClientID %>').val(data.d.DealerName); //option 2 
对于Html控件的使用: $(“#TextboxId”).val(“值”)

对于Asp.net文本框控件,请使用: $(“[id$=TextboxId]”)val(“值”)

对于Html控件的使用: $(“#TextboxId”).val(“值”)

对于Asp.net文本框控件,请使用:
$(“[id$=TextboxId]”)val(“值”)

为您的案例提供一些方法:

1.使用ClientID:

document.getElementById(“”)

2.将CssClass添加到文本框控件:

然后使用类选择器

3.只需在web.config中更改ClientMode:


内部

为您的案例提供了一些方法:

1.使用ClientID:

document.getElementById(“”)

2.将CssClass添加到文本框控件:

然后使用类选择器

3.只需在web.config中更改ClientMode:


内部

小提示:这不适用于编译时未包含在
.ASPX
页面中的Javascript。(即在外部JS文件中)小注:这不适用于编译时未包含在
.ASPX
页面中的Javascript。(即,在外部JS文件中)@RGraham:downvote被接受,但作为OP使用的
$.ajax
,这清楚地表明jquery库存在,所以用jquery编写有什么害处syntax@RGraham:downvote接受,但作为OP使用的
$。ajax
这清楚地表明jquery lib存在,所以使用jquery语法编写有什么害处
$("input[id$='tex_dealername']").val(data.d.DealerName);  //option 1
$('<%= tex_dealername.ClientID %>').val(data.d.DealerName); //option 2 
$('.yourClassName').val(data.d.DealerName);