使用JQuery从asp:textbox检索值 [英] Retrieve value from asp:textbox with JQuery

查看:79
本文介绍了使用JQuery从asp:textbox检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页上的表单中有一些asp:textbox控件,下面是一个代码段.第一个是输入接收者的字段,另一个是较大的文本区域,应将接收者的姓名以及其他一些文本加载到该文本区域中.

I have a few asp:textbox controls in a form on a webpage, below is a snippet. The first is a field where the recipient is entered, the other is a larger textarea where the recipients name should be loaded into, along with some other text.

<asp:TextBox name="recipient" ID="recipient" class="inputBox" onChange="addNames()" runat="server" />
<asp:TextBox TextMode="MultiLine" name="usermessage" ID="usermessage" class="usermessage" height="128" width="425px" runat="server"></asp:TextBox>

使用带有以下代码的JQuery,将标准消息加载到第二个文本框中:

A standard message is loaded into this second textbox with use of JQuery with this code:

$(".usermessage").val("Hello etc");

这很好,显示消息.
当用户在其他文本框中之一输入收件人的姓名或他自己的姓名时,将触发addNames().此功能将收件人的名称添加到用户消息框中的标准消息中.

This works nicely, the message is shown.
When a user enters the name of a recipient or his own name in one of the other textboxes, addNames() is triggered. This function adds the name of the recipient to the standard message in the usermessage box.

function addNames() {
    //update textbox
    var recipient = $(".recipient").val();
    var sender = $(".name").val();
    $(".usermessage").val("Hello " + recipient +", \nThis is a message. \n\rKind regards, \n" + sender);
    }

问题在于,收件人发件人这两个变量是未定义的".

Problem is that the two variables recipient and sender are "undefined".

你好未定义,
这是一条消息.

Hello undefined,
This is a message.

亲切的问候,
未定义

Kind regards,
undefined




实际问题:从asp:textbox检索值的正确代码是什么




Actual question: What is the correct code to retrieve the value from an asp:textbox if this

var recipient = $(".recipient").val();  

不起作用?

html中的输出如下:

The output in the html is as follows:

<input name="ctl00$contentPlaceHolderRightColumn$recipient" type="text" id="ctl00_contentPlaceHolderRightColumn_recipient" name="recipient" class="inputBox" onChange="addNames()" />

我正在将JQuery v1.3.2和Firefox v3.5.3.一起使用.

I'm using JQuery v1.3.2, with Firefox v3.5.3.

推荐答案

据我所知

var recipient = $(".recipient")

将选择具有CLASS收件人的所有dom元素.您的输入框具有"inputBox"类.

Will select all dom elements with a CLASS of recipient. Your input box has a class of "inputBox".

您需要使用#

所以:

var recipient = $("#recipient")

但是您正在使用ASP.NET控件,这些控件赋予它在服务器上生成的唯一ID,因此它是唯一的. (在您的情况下为ctl00_contentPlaceHolderRightColumn_recipient)

But you are using ASP.NET controls, which give it a unique ID generated on the server so it's unique. (in your case it's ctl00_contentPlaceHolderRightColumn_recipient)

要选择,您需要做

var recipient = $("#<%=recipient.ClientID%>")

-编辑出一些语法错误

这篇关于使用JQuery从asp:textbox检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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