简体   繁体   中英

Identify Hidden Form Value Without an ID or Class

I am writing a Greasemonkey script and I need to be able to take the value from a hidden form element and set it to a variable.

The hidden form value looks like this:

 <input type="hidden" name="ASIN" value="B009MO89Y4" />

I have no ID, class, or any way I can see to set the "value" to a variable. This needs to work dynamically and I currently have no way to establish a class or ID to this value.

Is there a Javascript (or jQuery) method to set this?

In other words:

Find "input" with name "ASIN" and set .val() to a variable?

This selector and assignment:

$("input[name='ASIN']").val(); <---- returns value of that input

var inputVal = $("input[name='ASIN']").val(); <-- Assigns it

var temp = "Stuff";
$("input[name='ASIN']").val(temp); <----Assigns the value of the temp var.

You can select it via. name in jQuery like so:

var bar = "Example"; // Example text, to be used in val().
var x = $('input[name="ASIN"]').val(bar);  
// Sets the variable x to be the value bar for the input with the name ASIN.

Here's a working jQuery jsFiddle .


In pure Javascript * :

var bar = "Example";  
document.getElementsByName("ASIN")[0].value = bar;

Here's a working Javascript jsFiddle .


* Please note that although document.getElementsByName is supported well in Firefox, Chrome and Safari, it has limited browser support. in IE and Opera.

您可以使用jQuery 属性equals选择器

$('input[name="ASIN"]').val(foo);

Like this:

 $('input[name="ASIN"]').val();

Var:

var hiddenAsin = $('input[name="ASIN"]').val();

您可以使用任何属性过滤选择。

$('input[name=ASIN]').val("New Value")

You can use selector that targets inputs of type hidden. It should look like that:

$('input[type=hidden]');

or simpler:

$(':hidden');

For pure javascript:

Try document.getElementsByName('name') .
Note that cmptrgeekken pointed out that this has limited browser-support (although that would not be an issue with greasemonkey in FF).

As an alternative, if that hidden element has a fixed place you could also access it by index-number in a predictable collection that you got from knownParent.getElementsByTagName('tag')[#] (So the first hidden inputtag inside a form would be number 0).

Another variation is to get (again) knownParent.getElementsByTagName('tag') and loop over that collection to see what element has the 'name' attribute set that you seek. Simply do:

var target=knownParent.getElementsByTagName('input'), L=target.length;
           while(L--){ if(target[L].name==='name'){target=target[L]; break;} }
alert(target.value); //target is now the element you seek.

Example fiddle here .

Good luck!

Use this method

var inputs = document.getElementsByTagName('input');
for(var i = 0...)
{
    //go through each input and look for the name "ANSI" and the type is hidden.

     //and do your changes. 
}

this is for javascript remember.

with this you should be able to get that specific hidden form without an ID nor a Class assigned to that specific form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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