Adding partial views dynamically into the main view on button click like the following , dynamically loaded views input failed to attach jQuery on main view but they this attachment work perfectly on partial views.
Main view
<input type="button" name="BtnAddNew" value="Add New" onclick="AddCustomer();" />
<script>
function AddCustomer() {
$.get("DataEntryScreen").done(function (r) {
$('#DivDatEntry').html(r);
});
}
$('#btnCancel').click(function (e) {//not work on main view
alert();
$('#DivDatEntry').html("");
});
</script>
Partial view=DataEntryScreen.cshtml
@{
AjaxOptions o = new AjaxOptions();
o.OnSuccess = "OnSaveSuccess";
}
@using (Ajax.BeginForm("SaveCustomer", "SPA", o))
{
<table>
<tr>
<td>Customer Name:</td>
<td><input type="text" name="CustomerName" value="" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="Address" value="" /></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="Age" value="" /></td>
</tr>
<tr>
<td colspan="3" align="right">
<input type="submit" name="name" value="Save" />
<input type="button" name="name" value="Reset" />
<input id="btnCancel" type="button" name="name" value="Cancel" />
</td>
</tr>
</table>
}
Now dynamically loaded partial view="DataEntryScreen.cshtml" button="btnCancel" attach jQuery event on the main view is not work, then writing the same jQuery syntax on correspondent partial view work perfectly.
<script>
$('#btnCancel').click(function (e) {//perfectly work on partial view.
alert();
$('#DivDatEntry').html("");
});
</script>
Why jQuery event attachment not work on main view?