ASP textbox calls javascript function

I have a search text box in asp. And I want him to send a request to the server every time the text was changed there. I have a javascript function that sends a request, but it is not called every time I enter something into a text field. How can I call a javascript function from an ASP text field?

This is my text box:

<asp:TextBox ID="search" name="Search" runat="server" onchange="javascript:text_changed();"></asp:TextBox>

This is my js function:

function text_changed() {
     searchedword = document.getElementById("ContentPlaceHolder1_search").value;
     SendRequest();
}
+4
source share
5 answers

You must use the onKeyPress event to call the function.

<asp:TextBox ID="search" name="Search" runat="server" onKeyPress="javascript:text_changed();"></asp:TextBox>
+7
source

Shivam's answer is correct. You can use the KeyPress event to receive strokes using this event.

, ASP.NET:

document.getElementById("ContentPlaceHolder1_search").value;

, - html, ASP.NET id.

:

function text_changed(textObj) {
     searchedword = textObj.value;
     SendRequest();
}

<asp:TextBox ID="search" name="Search" runat="server" 
  onKeyPress="javascript:text_changed(this);"></asp:TextBox>
+2

, ,

onkeyup onkeydown.

. , , .

    $("#search").keydown(function(){
    text_changed();
  });
  $("#search").keyup(function(){
    text_changed();
  });

0

:)

$("#search").change(function(){
      //your ajax codes
});
0
<script>
function multy(val)
{
alert(val.value+"--"+val.id);
}
</script>
<input id="txtdemo" type="text" onchange="multy(this);"></br>
<input id="txtdemo" type="text" onchange="multy(this);">
</input>

Click to view input screen

...:)

0

Source: https://habr.com/ru/post/1548746/


All Articles