[英]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. 我正在编写一个Greasemonkey脚本,我需要能够从隐藏的表单元素中获取值并将其设置为变量。
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. 我没有ID,类或任何可以看到的将“值”设置为变量的方式。 This needs to work dynamically and I currently have no way to establish a class or ID to this value.
这需要动态地工作,我目前无法建立该值的类或ID。
Is there a Javascript (or jQuery) method to set this? 是否有Javascript(或jQuery)方法来设置?
In other words: 换一种说法:
Find "input" with name "ASIN" and set .val() to a variable? 查找名称为“ ASIN”的“ input”并将.val()设置为变量?
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:
像这样在jQuery中命名:
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 . 这是一个工作的jQuery jsFiddle 。
In pure Javascript * : 用纯Javascript * :
var bar = "Example";
document.getElementsByName("ASIN")[0].value = bar;
Here's a working Javascript jsFiddle . 这是一个有效的Javascript jsFiddle 。
* Please note that although document.getElementsByName
is supported well in Firefox, Chrome and Safari, it has limited browser support. * 请注意,尽管Firefox,Chrome和Safari都很好地支持
document.getElementsByName
,但它对浏览器的支持有限。 in IE and Opera. 在IE和Opera中。
您可以使用jQuery 属性equals选择器
$('input[name="ASIN"]').val(foo);
Like this: 像这样:
$('input[name="ASIN"]').val();
Var: 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: 对于纯JavaScript:
Try document.getElementsByName('name')
. 尝试
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). 请注意,cmptrgeekken指出这对浏览器的支持有限 (尽管对于FF中的oilsmonkey而言这不是问题)。
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). 作为替代方案,如果该隐藏元素的位置固定,您还可以通过从
knownParent.getElementsByTagName('tag')[#]
获得的可预测集合中的索引号访问该元素(因此,表单中的第一个隐藏输入标签会是数字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. 另一个变体是(再次)获得
knownParent.getElementsByTagName('tag')
并遍历该集合,以查看哪些元素具有您要查找的'name'属性集。 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. 这是针对javascript的。
with this you should be able to get that specific hidden form without an ID nor a Class assigned to that specific form. 有了它,您应该能够获得该特定的隐藏表单,而无需为其分配ID或类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.