简体   繁体   中英

Page is refreshing on button click while I'm stoping it using JQuery (event.preventDefault method) - Asp.Net MVC

I have created a table element and appended it to a div element using jquery.

My code is like:

var element="<table id='tbl'><tr><td><input type='text' id='txt'/></td>

<td><input type='button' id='close' value='Remove'/></td>
            </tr></table>";

$('#div1').append(element);

And on the button click event of close button I've write the code :

$('#close').click(function(e){
e.preventDefault();
$('#tbl').remove();
});

but as I press the remove button page is being refreshing.

Please help me out

You are binding the event, possibly at the time when the element is not in the DOM. You should use following code for dynamically inserted elements:

$('#close').on("click", function(e){ e.preventDefault(); $('#tbl').remove(); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM