简体   繁体   English

如何使用JQuery选择作为隐藏表单字段值数组一部分的隐藏表单字段值?

[英]How to use JQuery to select a hidden form field value that is part of an array of hidden form field values?

I am having trouble selecting a hidden form field value that is a part of an array of hidden form field values having the same name. 我在选择隐藏表单字段值时遇到了麻烦,该表单字段值是具有相同名称的隐藏表单字段值数组的一部分。 Here's what one element of the array looks like: 数组的一个元素如下所示:

<input data-val="true" data-val-number="The field ProductId must be a number." data-val-required="The ProductId field is required." id="OrderItemViewModels_2__ProductId" name="OrderItemViewModels[2].ProductId" type="hidden" value="3" />

I have tried selecting it several different ways in JQuery, such as: 我尝试在JQuery中通过几种不同的方式来选择它,例如:

$('#OrderItemViewModels[2].ProductId').val());

and.. 和..

$('#OrderItemViewModels\\[2\\].ProductId').val());

Neither of which worked. 两者都不起作用。 I've also tried a few other ways as well. 我也尝试了其他一些方法。 So, I tried some of the suggestions from below, hoping to just print the values in an alert. 因此,我尝试了以下建议,希望仅在警报中打印这些值。 I tried these: 我尝试了这些:

alert('valueDirect2=' + $('#OrderItemViewModels_2__ProductId')).val();
alert('valueDirect1=' + $('input[name="OrderItemViewModels[2].ProductId"]')).val();

This results in printing valueDirect2=[Object object] in both cases. 在两种情况下,这都导致打印valueDirect2 = [Object object]。 Why? 为什么? What is wrong here? 怎么了

I'm at a loss as to how to get these values. 我对如何获得这些价值一无所知。 Any help? 有什么帮助吗?

Do you need this to be dynamic, ie change on the fly which input field is selected? 您是否需要动态设置,即即时更改选择了哪个input字段? If so, you'll need to add a variable into the middle of your selector. 如果是这样,则需要在选择器的中间添加一个变量。

If you modify your existing code like this, it'll work: 如果您像这样修改现有的代码,它将起作用:

$('#OrderItemViewModels_2__ProductId').val();

Your two samples didn't work because you're formatting your selector string similar to how the name attribute, but the # prefix tells jQuery to look for an id attribute. 您的两个示例无效,因为您正在格式化选择器字符串,类似于name属性,但是#前缀告诉jQuery查找id属性。

It is possible to select an element by name as well, which would look like this: 也可以通过名称选择元素,如下所示:

$('input[name="OrderItemViewModels[2].ProductId"]').val();

In your alert examples, you're seeing [Object object] because the $ syntax in jQuery is a shortcut for calling the jQuery function. 在您的alert示例中,您看到的是[Object object]因为jQuery中的$语法是调用jQuery函数的快捷方式。 What you're really doing is this: 您真正在做什么是这样的:

jQuery('input[name="OrderItemViewModels[2].ProductId"]').val();

It's just a plain old function call. 这只是一个普通的旧函数调用。 The S is a convenient shorthand. S是方便的简写。 You're calling a function and passing in a string, which is the selector that jQuery will evaluate. 您正在调用一个函数并传入一个字符串,这是jQuery将评估的选择器。

Your selector wasn't returning anything because it was formatted incorrectly, so you were ending up printing the .val() , or value, of the jQuery function itself. 您的选择器未返回任何内容,因为其格式不正确,因此您最终将打印jQuery函数本身的.val()或值。 It's a little confusing until you have a good conceptual grasp of how jQuery works. 直到您对jQuery的工作原理有了很好的了解之前,这有点令人困惑。

To make your selector dynamic, just splice strings together around the variable that contains the number of the input you want to select: 为了使选择器具有动态性,只需将包含要选择的input编号的变量周围的字符串拼接在一起:

$('input[name="OrderItemViewModels[' + myVariable + '].ProductId"]').val();

Here's a jsFiddle that illustrates both options. 这是一个jsFiddle ,说明了这两个选项。

$('input[name="OrderItemViewModels['+ index +'].ProductId"]').val(); // index = 0,1,2...

要么

$('#OrderItemViewModels_'+ index +'__ProductId').val(); // index = 0,1,2...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM