ASP.NET does not work jquery on my button

0

I have a button which I want to perform a preventdefault, the click function associated with that button does not work for me, to try I have the following:

<script src="../js/jQuery-2.1.4.min.js" type="text/javascript"</script>

<script type="text/javascript">
$(document).ready(function () {
    $('#btnGuardar').click(function () {
        alert("Es una prueba");
    });
});

</script>

and the button

<asp:Button ID="btnGuardar" runat="server" Text="Button" />

It turns out that if I change '#btnSave' for the element "a" IT WORKS for what would be links, then how can it be that simply changing the a for my button does not work? and I think the question may come because my webform is associated with a masterpage, since creating an independent webform I tested the code for the button and it works for me.

    
asked by Marcelo 20.07.2016 в 22:51
source

1 answer

0

To be able to select an asp.net button in query you should use

$('#<%=btnGuardar.ClientID%>').click(function () {

or else

$("[id*='btnGuardar']").click(function () {

the latter searches for controls by id but validates if it contains the name in an approximate way.

Attribute Contains Selector [name *="value"]

Remember that asp.net changes the ids when you render the control, it is more inspected the html generated using the Developer tools (which you access with F12) and you will see the id that is really assigned to the button

    
answered by 20.07.2016 / 23:08
source