通过jquery获取mvc webgrid隐藏字段值 [英] get mvc webgrid hidden field value by jquery

查看:66
本文介绍了通过jquery获取mvc webgrid隐藏字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我使用下面的代码,我想得到hdn1的值(隐藏字段)

hello,
I used the following code and I want get value of hdn1(hidden field)

var grid = new WebGrid(Model, rowsPerPage: 15, ajaxUpdateContainerId: "myGrid");
  @grid.GetHtml(tableStyle: "clrPreview", columns: grid.Columns(
  grid.Column("CityName", "City", canSort:false),
                                              grid.Column("HotelName", "Hotel", canSort: false),
                                              grid.Column("HotelCategoryName", "Category", canSort: false),
                                              grid.Column("GitRangeStartFrom", "Git Range Start From", canSort: false),
                                       grid.Column(columnName: "Action", canSort: false,
                                          format: @<text><input type="hidden" id="hdn1" value='@item.HotelId'  />
  @Html.ActionLink("Edit", "Edit", new { id = item.HotelId }, new { rowid = item.HotelId })
  @* |  @Html.ActionLink("Delete", "Delete", new { id = item.HotelId })*@
  </text>)



我想通过jquery获取隐藏字段值


I want to get hidden field value by jquery

推荐答案

你的问题是你不能拥有多个元素相同的ID。但是使用您用于该列的模式,您将生成具有相同ID的输入。这不会起作用,因为除了违反html标准之外,jquery无法抓住它。

您可以使用更多方法:

- 只需在该输入元素中添加一个类,而不是ID。如果你已经有了这一行,你可以抓住那个班级的孩子 - 这将是你想要的输入。或者,如果您在该行中有任何其他元素,则可以使用jquery dom导航工具来获取该元素。如果它是行中唯一隐藏的元素输入,你可能甚至不需要该类。

- 使用模型中的id( @ item.HotelId )生成ID

如果你需要行的其他列上的id(并忘记隐藏的输入):

- 使用样式 WebGridColumn的属性,用于添加一个包含模型ID的特殊类

- 使用格式你使用的属性,但将id作为自定义html属性添加到你生成的元素



所有这些都可以使用,可能更多,但你必须选择基于你想用这个值做什么。
Your problem is that you can not have multiple elements with the same ID. But with the pattern you use for that column you will generate inputs with the same ID. That won''t work, since besides the fact that you violate the html standard, jquery can not grab it.
You have could use more approaches:
- just add a class to that input element, and no ID. And if you have already the row, you can grab it''s child with that class - and that will be the input you want. Or if you have any other element in that row you can use the jquery dom navigation tools to get that. If it is the only hidden element input in the row you will probably don''t need even the class.
- use the id from the model (@item.HotelId) to generate ID
If you need the id on the other columns of the row (and forget the hidden input):
- use the Style property of WebGridColumn to add a special class that holds the model id
- use the Format property as you used but add id as custom html property to the element you generate

All these can be used, an probably more, but you have to choose based on what you want to do with that value.


var hv =


' #hdn1')。val();
('#hdn1').val();





应该这样做。



Should do the trick.


这篇关于通过jquery获取mvc webgrid隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆