programmer138200 programmer138200 - 3 months ago 23
jQuery Question

How to bind click event of button into PartialView (MVC)?

I make a page in asp.net MVC .In this page exist a View and a PartialView.I put button into partialview and write Countinue function into View.

button:

<button type="button" class="btn btn-warning center-block btn-large" id="Continue" onclick="Continue(this);">Continue</button>


js:

var Continue=function (e) {
e.preventDefault();
if ($('#FieldsetCharterFlightsReturn article').size() < 1) {
if (validateDeparturef(e)) {
$("#CharterFlightsForm").submit();
}

}
if ($('#FieldsetCharterFlightsReturn article').size() > 0) {
if (validateDeparturef(e) && validateArrivalf(e)) {
$("#CharterFlightsForm").submit();
}
}
};


Now When i click on the button get this error:


TypeError: Continue is not a function


What is right way to bind event of elements into PartialView?

Answer

Since you are using bind event using it instead of ugly-inline click handler.

 $(function(){
    $('#Continue').on('click', Continue);
 })

OR, Using Event delegation

 $(document).on('click', '#Continue', Continue);

And remove inline onclick="Continue(event);" handler