我需要知道是否可以这样做。我有一个 MVC 应用程序,我在其中循环一些数据并为每个对象输出一个表单。在提交项目的表单上,它会发布到服务器并返回更新的 View 。我想用新的 View 数据替换当前的 html。
问题是我无法弄清楚如何在成功函数上提交的表单上下文中获取父元素、子元素或特定元素。我也尝试过 $(this).parent、$(this).child 等。它将表单视为一个对象,而不是一个 html 元素,所以我猜这些查询函数不起作用。
是否可以做到这一点,或者我是否需要添加某种类型的 hack,以便我可以在 Success 函数中获取唯一的元素 id,而不是尝试使用 $(this)?
<script type="text/javascript">
$(function () {
$("#form").live("submit", function (e) {
$.post($(this).attr("action"),
$(this).serialize(),
function (response) {
var form = $(this);
var container = $("#itemContainer", form);
container.html(response);
});
e.preventDefault();
});
});
</script>
最佳答案
<script type="text/javascript">
$(function () {
$("#form").live("submit", function (e) {
$.post($(this).attr("action"),
$(this).serialize(),
$.proxy( function (response) {
var form = $(this);
var container = $("#itemContainer", form);
container.html(response);
}, this ) );
e.preventDefault();
});
});
</script>
关于javascript - 可以在提交函数中使用 jQuery 获取提交表单的子元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8839713/