部分视图点击事件不会激发 [英] Partial View click event not fire
问题描述
我的* .aspx页面(主页):
My *.aspx Page (main page):
function getEmployeeHours(employeeId,year,month) {
$('#clocked-details').load('/Employees/GetEmployeeClockedHours/',{ 'employeeId': employeeId,'year': year,'month': month });
};
我的部分视图* .ascx:
My partial view *.ascx :
<td>
<button id="btnSave" type="button" class="actionButton">
Save</button>
</td>
如上面的代码片段,我需要从主视图中获取部分视图 btnSave 触发点击事件。
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;
});
但它没有触发。我在使用加载 jquery方法以异步方式加载我的局部视图。
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 ?
推荐答案
由于包含按钮的用户控件是动态加载的,因此可以使用(#btnSave)。live(click){...}
jquery事件,同时将脚本保留在主视图中,这可以跟踪将来添加到DOM的控件。因此,只要出现与live(..)匹配指定选择器的控件,该事件就会自动换行。
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.
希望这对您有帮助。
这篇关于部分视图点击事件不会激发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!