0

你好,

我有以下用于调用 ASP.NET MVC 控制器方法的 jquery 代码,该方法返回使用部分视图创建的 html。

$("form[action$='ShowProperties']").submit(function() {
$.post($(this).attr("action"), $(this).serialize(), function(response) {

        $(this).children("div.serviceproperties").html(response);
       // $("div.serviceproperties").html(response);

    });
    return false;
});

呈现的 html 如下所示:

<ul>
<li class="serviceactionitem">
    <form method="post" action="/Service/ShowProperties">
        <input type="submit" value="Show General" name="[Show]"/>
        <input id="id" type="hidden" value="1" name="id"/>
        <input id="serviceitem" type="hidden" value="general" name="serviceitem"/>
        <div class="serviceproperties"> </div>
    </form>
</li>
<li class="serviceactionitem">
    <form method="post" action="/Service/ShowProperties">
        <input type="submit" value="Show Specific" name="[Show]"/>
        <input id="id" type="hidden" value="1" name="id"/>
        <input id="serviceitem" type="hidden" value="specific" name="serviceitem"/>
        <div class="serviceproperties"> </div>
    </form>
</li>

响应是需要在表单元素内部的 div 元素内设置的 html。我在页面上有几个表单元素,我不知道如何更新正确表单内的 div。注释代码,当然会更新页面上每个表单的 div。

主要是 $(this) 不返回表单元素,这可能是正常的。因此,我需要一种方法来处理处理响应的函数内部的正确表单元素,并在其中查找和更改 div。

谢谢

4

1 回答 1

1

只需重新选择表格:

 $("form[action$='ShowProperties'] div.serviceproperties").html(response);

或者先捕获元素:

$("form[action$='ShowProperties']").submit(function() {
var $form = $(this);

$.post($(this).attr("action"), $(this).serialize(), function(response) {

        $("div.serviceproperties",$form).html(response);

    });
    return false;
});
于 2009-09-09T10:55:04.330 回答