且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过jquery获取mvc webgrid隐藏字段值

更新时间:2023-02-11 20:34:53

你的问题是你不能拥有多个元素相同的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.