序列化输入数组时如何删除编码的字符?

时间:2019-02-12 15:27:37

标签: jquery ajax asp.net-mvc

我的局部视图包含以下内容:

<div class="multiplePaths">
  @for (var i = 0; i < Model.Count; i++)
  {
     <div class="form-group">
        @Html.HiddenFor(model=>model[i].Label)
        @Html.EditorFor(model => model[i].Path, new {@class = "form-control col-md-10"})
     </div>
  }
</div>

当我调用此jQuery时:

var model = $(".multiplePaths :input").serialize();

结果看起来像这样:

%5B0%5D.Label=Repertoire%20destination&%5B0%5D.Path=

1 个答案:

答案 0 :(得分:3)

格式化是因为serialize()用于输出查询字符串。因此,特殊字符(例如[])已进行URI编码。

要获得所需的输出,您只需再次对其进行URI解码:

var model = $(".multiplePaths :input").serialize();
console.log('encoded:', model);

var decodedModel = decodeURI(model);
console.log('decoded:', decodedModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="multiplePaths">
  <div class="form-group">
    <input type="hidden" name="[0].Label" value="fooLabel_1" />
    <input type="text" name="[0].Path" value="fooPath_1" />
  </div>
</div>