从行动,通过AJAX在ASP.NET MVC中获取的HTML数据后更改输入名称的属性属性、名称、行动、数据

2023-09-10 22:03:17 作者:莈幵始僦結涑

我有一个从服务器,得到一个简单的Ajax请求生成的HTML,如:

I have a simple ajax request which get from server a generated HTML, like:

$.ajax({
    url: '/GetData'
    type: "POST",
    dataType: "html",
    data: ...,
    success: function(data) {
        // here I want to change `name` attributes of inputs
        // before print on page
        // but it doesn't work, so, how to manage this ?
        $(data).find("input[name='test']").prop("name", "anotherValue");

        $("myDiv").prepend($(data));
    }
});

和我的动作很简单:

[HttpPost]
public ActionResult GetData(){
    return PartialView("myview", new MyModel());
}

我想更改输入名称属性之前打印出来的HTML页面。如果我在成功函数(见上文),则不会进行任何更改。

I want to change input name attributes before print them in html page. If I do in success function (see above) then no change is made.

为什么呢?为了实现这一目标?

Why ? To to achieve this ?

推荐答案

尝试像

$("input").each(function() {
    if($(this).prop("name") == "test") $(this).prop("name", "anotherValue");
});