add text to an asp.net textbox control with jQuery

后端 未结 1 1622
无人共我
无人共我 2021-01-06 15:19

Within an web form I have a some input fields

Label 1
相关标签:
1条回答
  • 2021-01-06 16:22

    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" />
    

    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");
    

    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" />
    

    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!");
    

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

    EDIT

    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());
        }
    });
    
    0 讨论(0)
提交回复
热议问题