Partial View click event not fire

2019-07-20 15:54发布

My *.aspx Page (main page):

function getEmployeeHours(employeeId,year,month) {
 $('#clocked-details').load('/Employees/GetEmployeeClockedHours/',{ 'employeeId': employeeId,'year': year,'month': month });
    };

My partial view *.ascx :

 <td>
   <button id="btnSave" type="button" class="actionButton">
     Save</button>
  </td>

As above code snippet I need to accsess partial view btnSave from main view to trigger click event.

I have written below code inside the main view.

$('#btnSave').off('click').on('click', function () {

         var yearValue = $("#year").val();
         var monthValue = $("#month").val();

         $.ajax({
             url: "/Employees/UpdateEmployeeClockedHoursByProvider",
             type: 'POST',
             cache: false,
             data: { employeeId: employeeId, year: yearValue, month: monthValue },
             success: function (result) {

             },
             error: function (xhr, ajaxOptions, thrownError) {
                 alert(xhr.status);
                 alert(thrownError);
             }
         });

         return false;
     });

But it doesn't fire.Here I am using load jquery method to load my partial view asynchronously.

So my question is how should I fire click event of partial view ?

2条回答
神经病院院长
2楼-- · 2019-07-20 16:15

Since the usercontrol containging the button is being loaded dynamically, you can use ("#btnSave").live("click"){...} event of jquery while keeping the script in the main view, which can keep track of the controls being added to the DOM in future. So whenever a control matching the specified selector for "live(..)" appears, the event is wiredup automatically.

Hope this help you.

查看更多
小情绪 Triste *
3楼-- · 2019-07-20 16:35

Since your html is being loaded into the page dynamically you'll have to give more context to your DOM query.

Try this.

$(document).on('click', '#bntSave', function(){
  //do stuff
});
查看更多
登录 后发表回答