添加文字与jQuery一个asp.net文本框控件 [英] add text to an asp.net textbox control with jQuery

查看:97
本文介绍了添加文字与jQuery一个asp.net文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个网页表单我有一些输入字段

Within an web form I have a some input fields

<div id="search1">
        <div class="forminput">
            Label 1<br />
            <input type="text" name="Field1" value="" size="20" />
        </div>
        <div class="forminput">
            Label 2<br />
            <input type="text" name="Field2" value="" size="20" />
        </div>
</div>

由于重点是在失去控制输入的值需要返回到一个asp.net文本框控件。我已经能够使用以下code的值传递给一个div层或标准的HTML元素:

As focus is lost on the control the value entered needs to be returned to an asp.net textbox control. I have been able to pass the values to a div layer or standard html element using the following code:

    function fieldUpdate() {

            var currentValue = $(this).val();
            var field = $(this).attr("name");

                if (currentValue != "") {
                    $("#results").append(field + ":" + currentValue + "+");
                }
        }
 $(function () {
            $("#search1 :input[type=text]").focusout(fieldUpdate);

        });

不过,这似乎并没有与一个asp.net服务器端控制工作。
更新
这个问题的目的是提供进入标准的HTML输入字段到一个asp的值:文本框控件

However this does not seem to work with an asp.net server side control. Update The objective of this question is to provision the values entered into standard html input fields into an asp:textbox control

<asp:TextBox ID="results2" Text="" Width="400" runat="server" />

更新
下面是目前的解决方案是如何工作的屏幕截图。在顶部,你看到德输入字段作为一个用户输入的值和突出这个值被加到一个div层标记的结果中的每个字段的。我想要做的是,而不是添加字段名和值DIV元素我希望他们不是被添加到ASP:TextBox控件。

Update Below is a screenshot of how the current solution works. At the top you see teh input fields as a user enters a value and tabs out of each field this value is added to a Div layer labeled results. What I want to do is instead of adding the fieldname and value to a DIV element I want them to instead be added to a asp:textbox control.

有pferred有存储在外部的js文件中VS aspx页面内该code $ P $。

It is preferred to have this code stored within an external js file vs within the aspx page.

任何人都可以提供关于如何做到这一点有什么建议?

Can anyone provide any suggestions on how to accomplish this?

在此先感谢

更新的解决方案
感谢詹姆斯帮助我,虽然这方面的工作。下面是当前实现中使用jQuery的code。

Update Solution Thanks to James for helping me work though this. Below is the jQuery code as used in the current implementation.

function fieldUpdate() {

            var currentValue = $(this).val();
            var field = $(this).attr("name");
            var txt = $("#<%=results2.ClientID%>");
                if (currentValue != "") {
                    $("#results").append(field + ":" + currentValue + "+");
                    if (txt) {
                        txt.val(txt.val() + field + ":" + $(this).val() + "+");
                    }
                }
        }


        $(function () {
        //only evaluate input text field
            $("#search1 :input[type=text]").focusout(fieldUpdate);

这将现在允许一个asp:文本框的输入字段名和值进行填充。
实例字段1:富+字段2:somevalue2 +字段3:somevalue3 ...

This will now allow an asp:textbox to be populated with the input field name and value. Example Field1:foo+Field2:somevalue2+Field3:somevalue3 ...

推荐答案

这些都不是ASP.NET TextBox控件;他们是HTML文本输入。但要回答你的问题,你可以这样做:

Those are not ASP.NET TextBox controls; they're HTML text inputs. To answer your question though, you would do this:

<script type="text/javascript">
    $(function(){
        $("#input1").val("Hello world");
    });
</script>
<input type="text" id="input1" />

我注意到,你在你的例子设置名称,而不是ID的。我会用ID,如果你有一个选择,但你可以找到的名字像这样的元素:

I noticed that you're setting the name instead of the ID in your example. I would use ID if you have a choice, but you can find an element by name like this:

$("input[name='Field1']").val("Hello world");

如果您使用ASP.NET实际TextBox控件,该解决方案将是一个有点不同:

If you use an actual ASP.NET TextBox control, the solution will be a little different:

<script type="text/javascript">
    $(function(){
        $("#<%=TextBox1.ClientID%>").val("Hello world");
    });
</script>
<asp:TextBox ID="TextBox1" runat="server" />

如果你想获得的所有的在搜索1 DIV的文本输入框,你可以这样做:

If you want to get all of the text inputs in the search1 div, you can do this:

$(".forminput input[type='text']").val("Hello world!");

下面是一个对的jsfiddle上面的例子: http://jsfiddle.net/DWYTR/

Here's a jsFiddle for the above example: http://jsfiddle.net/DWYTR/

修改

根据您的评论,在这里是如何从一个输入到另一个模糊复制值:

Per your comment, here is how you can copy a value from one input to another on blur:

$(".forminput input[type='text']").blur(function(){
    var txt = $("#<%=TextBox1.ClientID%>");
    if (txt){
        txt.val(txt.val() + $(this).val());
    }
});

这篇关于添加文字与jQuery一个asp.net文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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