我们如何使用Asp.net中的jquery将值传递给textbox

时间:2022-12-02 10:04:23

I want to pass value in textbox by its name using jquery in asp.net but when i passed the value to textbox the value in textbox is visible for the short time then it disappered every time this is my code

我想在asp.net中使用jquery在文本框中传递值,但是当我将值传递给文本框时,文本框中的值在短时间内可见,然后每次这是我的代码时它就会消失

    <script type="text/jscript" src="Scripts/jquery-1.9.1.min.js"></script>
    <script>
    $(document).ready(function () {
    $("button").click(function () {
        $('input[name="myb"]').val("mynameabc")
    });
    });
    </script>

    <div id="di1" style="height: 150px">
    <button>ClickME</button>
    <asp:Button ID="Button1" runat="server" Text="Button" />

    <input id="Text1" name="myb" type="text" />

    </div>

1 个解决方案

#1


1  

You are getting a page refresh due to the asp:Button.

由于asp:Button,您正在刷新页面。

You need to do this:

你需要这样做:

$(document).ready(function () {
  $("button").click(function (ev) { //note new parameter here
    $('input[name="myb"]').val("mynameabc")
    ev.preventDefault(); //add this...keeps button from 'submitting' the page
  });
});

Alternatively, there is a "UseSubmitBehavior" attribute on asp:Button you could use as well...which approach you use may be dependent on what you're trying to accomplish on the page.

或者,您可以使用asp:Button上的“UseSubmitBehavior”属性...您使用的方法可能取决于您在页面上尝试完成的操作。

#1


1  

You are getting a page refresh due to the asp:Button.

由于asp:Button,您正在刷新页面。

You need to do this:

你需要这样做:

$(document).ready(function () {
  $("button").click(function (ev) { //note new parameter here
    $('input[name="myb"]').val("mynameabc")
    ev.preventDefault(); //add this...keeps button from 'submitting' the page
  });
});

Alternatively, there is a "UseSubmitBehavior" attribute on asp:Button you could use as well...which approach you use may be dependent on what you're trying to accomplish on the page.

或者,您可以使用asp:Button上的“UseSubmitBehavior”属性...您使用的方法可能取决于您在页面上尝试完成的操作。