Javascript怎么取得TextBox文本框的值?

xiaojiezizizi 2011-09-09 08:28:43

function txtonfocus() {
var txt = document.getElementById("txtSearch");
txt.vaklue = "123";
}


我在文本框得到焦点时触发这个方法.

为什么不可以呢??
...全文
1085 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
x363961461 2011-09-09
  • 打赏
  • 举报
回复
终于又有一个人懂了 。。。欣慰

为什么学习。net前都没先学下控件原理呢,都不看看他怎么来的,变成了什么呢。

panxunmiao1 2011-09-09
  • 打赏
  • 举报
回复
回复 拿分
红魔大卫 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 liuchaolin 的回复:]

HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml……
[/Quote]

楼主这个方法完全可以
jacie0617 2011-09-09
  • 打赏
  • 举报
回复
我怎么觉得你不是取值,而是赋值吧。你的赋值应该没有什么问题。
剩下的应该就是你的事件触发的问题了。楼上的代码都可以实现你要说的调用问题了。
flybisu 2011-09-09
  • 打赏
  • 举报
回复
function txtonfocus()
{
var t=document.getElementById("<%=this.txtSearch.ClientID%>");
t.value="123";
}
posefooler 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 posefooler 的回复:]

引用 17 楼 posefooler 的回复:

测试过的,11楼的方法可行。
HTML code

<script language="javascript" type="text/javascript">
function txtOnfocus() {
document.getElementById("txt1").onfocus = function () { this.va……
[/Quote]

<asp:TextBox ID="TextBox1" Name="111" runat="server" onfocus="txtOnfocus()"></asp:TextBox>
奔四在望 2011-09-09
  • 打赏
  • 举报
回复
txt.value="123";
posefooler 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 posefooler 的回复:]

测试过的,11楼的方法可行。
HTML code

<script language="javascript" type="text/javascript">
function txtOnfocus() {
document.getElementById("txt1").onfocus = function () { this.value = "123"; };
……
[/Quote]


<script language="javascript" type="text/javascript">
function txtOnfocus() {
document.getElementById("<%=this.TextBox1.ClientID %>").onfocus = function () { this.value = "123"; };
}
</script>
<input type="text" name="txt1" id="txt1"/>
yksyuan 2011-09-09
  • 打赏
  • 举报
回复

document.getElementById("txtSearch").value;
posefooler 2011-09-09
  • 打赏
  • 举报
回复
测试过的,11楼的方法可行。

<script language="javascript" type="text/javascript">
function txtOnfocus() {
document.getElementById("txt1").onfocus = function () { this.value = "123"; };
}
</script>
<input type="text" name="txt1" id="txt1"/>
xingxingbanyue 2011-09-09
  • 打赏
  • 举报
回复

<head runat="server">
<title>无标题页</title>
<script type="text/ecmascript">
function txtonfocus()
{
var t=document.getElementById("<%=this.txtSearch.ClientID%>");
t.value="123";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox></div>
</form>
</body>
</html>



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.txtSearch.Attributes.Add("onfocus", "txtonfocus()");
}
}
xingxingbanyue 2011-09-09
  • 打赏
  • 举报
回复

<head runat="server">
<title>无标题页</title>
<script type="text/ecmascript">
function txtonfocus()
{
var t=document.getElementById("<%=this.txtSearch.ClientID%>");
t.value="123";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox></div>
</form>
</body>
</html>



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.txtSearch.Attributes.Add("onfocus", "txtonfocus()");
}
}
【Help】 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangwenquan123 的回复:]
txt.vaklue = "123";
============================
txt.value="123";
[/Quote]up....
RUNBEAR 2011-09-09
  • 打赏
  • 举报
回复
document.getElementById("<%= txtSearch.ClientID %>").onfocus = function () {
this.value = "123";
}

这个是可以的。

asp.net服务器端控件在客户端的ID变了。

你分别
alert( document.getElementById("txtSearch").length);
alert( document.getElementById("<%= txtSearch.ClientID %>").length);
看看。
xiaojiezizizi 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hou306010849 的回复:]

function txtonfocus() {
var txt = document.getElementById("txtSearch").value;

}
[/Quote]
也不行
md5e 2011-09-09
  • 打赏
  • 举报
回复

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
</div>
</form>
<script>
document.getElementById("<%= txtSearch.ClientID %>").onfocus = function () {
this.value = "123";
}
</script>
</body>
</html>

Im_Sorry 2011-09-09
  • 打赏
  • 举报
回复
function txtonfocus() {
var txt = document.getElementById("txtSearch").value;

}
xiaojiezizizi 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xingxingbanyue 的回复:]

HTML code

<head runat="server">
<title>无标题页</title>
<script type="text/ecmascript">
function txtonfocus()
{
var t=document.getElementById("txtSearch");
t.value="123";
……
[/Quote]

我需要用asp.net 的TextBox控件....
xingxingbanyue 2011-09-09
  • 打赏
  • 举报
回复

<head runat="server">
<title>无标题页</title>
<script type="text/ecmascript">
function txtonfocus()
{
var t=document.getElementById("txtSearch");
t.value="123";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type=text id="txtSearch" onfocus="txtonfocus()" />
</div>
</form>
</body>
</html>

xiaojiezizizi 2011-09-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 accomp 的回复:]

确定 ,走到这步了么 ?
[/Quote]
确定.
加载更多回复(6)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧