Partial View click event not fire

最后都变了- 提交于 2019-12-10 20:33:57

问题


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 ?


回答1:


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.




回答2:


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
});


来源:https://stackoverflow.com/questions/15437007/partial-view-click-event-not-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!