相关疑难解决方法(0)

我如何使用jQuery的form.serialize但排除空字段

我有一个搜索表单,其中包含许多通过GET提交的文本输入和下拉菜单.我想通过在执行搜索时从查询字符串中删除空字段来获得更清晰的搜索URL.

var form = $("form");  
var serializedFormStr = form.serialize();  
// I'd like to remove inputs where value is '' or '.' here
window.location.href = '/search?' + serializedFormStr
Run Code Online (Sandbox Code Playgroud)

知道如何使用jQuery做到这一点?

javascript forms jquery serialization input

104
推荐指数
4
解决办法
8万
查看次数

提交表单时不要包含空参数

我的控制器上的索引方法如下所示:

public ActionResult Index(string search, string sort, int? groupId)
Run Code Online (Sandbox Code Playgroud)

对于搜索功能,我有以下形式:

@using (Html.BeginForm())
{
    <div>
        @Html.Label("search", "Search")
        @Html.TextBox("search", ViewBag.Search as string)
        @Html.Hidden("sort", ViewBag.Sort as string)
        @Html.Hidden("groupId", ViewBag.GroupId as int?)
        <input type="submit" value="Search" />
    </div>
}
Run Code Online (Sandbox Code Playgroud)

Viewbag.Search,ViewBag.SortViewBag.GroupId包含最后使用的参数.这些可以是null"",当它们是,这是我在使用搜索表单时看到的URL:

...?search=foo&sort=&groupId=
Run Code Online (Sandbox Code Playgroud)

如何从URL隐藏这些空参数,看起来像...?search=foo什么?


编辑:正如Jason Nesbitt所说,您可以禁用隐藏字段以将其从表单中排除.但是,我还想隐藏来自除hidden字段之外的其他内容的空参数,例如常规input字段和select列表.

html asp.net url asp.net-mvc razor

3
推荐指数
1
解决办法
3014
查看次数

标签 统计

asp.net ×1

asp.net-mvc ×1

forms ×1

html ×1

input ×1

javascript ×1

jquery ×1

razor ×1

serialization ×1

url ×1