Following partial view, that has an input submit button, is successfully rendered via an Ajax call. Now, I want to make a different Ajax call when the input submit button inside the Partial view is clicked. But, the following client-side click event of this button is not firing. Instead, when I click on the Go
button, it directly calls the action method in the controller (bypassing the client side click event and hence not making my Ajax call shown below). For test I've put an alert("Test")
before the Ajax call in the code below. But the alert is not firing, why? NOTE: Ajax script is in the same view where partial view is rendered.
Partial View:
@model myProj.Model.TestViewmodel
<form id="frmID" asp-controller="myController" asp-action="TestAction" method="get">
List of Proj:<select asp-for="SelectedProjYr" asp-items="Model.lstProjYears"></select> <button type="submit" id="BtnGO" value="UpdateProjBtnGo" class="btn btn-default">GO</button>
</form>
Ajax Call when above button is clicked:
$(document).ready(function () {
$("#BtnGO").click(function () {
var btnVal = $(this).val();
alert("Test");
$.ajax({...
...
...
});
});
UPDATE:
Following this jquery doc on Event Delegation, I tried the following but still alert('Test')
does not popup. NOTE: Here #frmID
is the id of the form that is a parent of BtnGO
. Moreover, I've tried both with or without event.preventDefault();
line. I've also change button type from submit
to button
$('#frmID').on('click', '#BtnGO', function (event) {
event.preventDefault();
alert('Test');
});