在下面的代码中,单击“保存”按钮不会触发。
任何想法为什么?
[code]
<script type="text/javascript">
$(function () {
$(".Join").live("click", function () {
var html = "<tr><td colspan='3'>Enter Password: <input type='password' id='pwd' />";
html += " <input type='button' class='Save' value='Save' /></td></tr>";
$(this).closest("tr").after(html);
return false;
});
$(".Save").live("click", function () {
var json = { Password: $(this).closest("#pwd").val() };
$.ajax({
url: "/Group/ConfirmPassword",
type: "POST",
dataType: 'json',
data: JSON.stringify(json),
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.Success == true)
window.location.href = result.Url;
else
alert(result.ErrorMessage);
return false;
}
});
});
});
[/代码]
答案 0 :(得分:4)
因为当您为$ (".Save")
点击创建事件处理程序时,它不存在。使用:
$(function () {
$(".Join").live("click", function () {
var html = "<tr><td colspan='3'>Enter Password: <input type='password' id='pwd' />";
html += " <input type='button' class='Save' value='Save' /></td></tr>";
$(this).closest("tr").after(html);
$(".Save").live("click", function(){
var json = { Password: $(this).closest("#pwd").val() };
$.ajax({
url: "/Group/ConfirmPassword",
type: "POST",
dataType: 'json',
data: JSON.stringify(json),
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.Success == true)
window.location.href = result.Url;
else
alert(result.ErrorMessage);
return false;
}
});
});
return false;
});
});
修改强> 基于jsFiddle示例中的发现...将您已加载的代码更改为JoinList:
$("#grouplist").load(
AppRoot + "Group/JoinList/",
{ page: page_num },
function(){
$(".Join").live("click", function () {
var html = "<tr><td colspan='3'>Enter Password: <input type='password' id='pwd' />";
html += " <input type='button' class='Save' value='Save' /></td></tr>";
$(this).closest("tr").after(html);
$(".Save").live("click", function(){
var json = { Password: $(this).closest("#pwd").val() };
$.ajax({
url: "/Group/ConfirmPassword",
type: "POST",
dataType: 'json',
data: JSON.stringify(json),
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.Success == true)
window.location.href = result.Url;
else
alert(result.ErrorMessage);
return false;
}
});
});
return false;
});
}
);