从隐藏字段获取值-JavaScript
我有一个从隐藏字段获取值-JavaScript,javascript,jquery,Javascript,Jquery,我有一个 如何在不使用jQuery的情况下获取值 使用jQuery,我只写: var parse = $('.Key').attr("value") alert(parse); 我需要纯JavaScript,也许用正则表达式?我将在txt文件上执行这个脚本,该文件将包含这样的行 轻松!只需使用getElementsByClassName。例如: document.getElementsByClassName('Key')[0].value 或者,如果必须按id获取值,则可以使用getElem
如何在不使用jQuery的情况下获取值
使用jQuery,我只写:
var parse = $('.Key').attr("value")
alert(parse);
我需要纯JavaScript,也许用正则表达式?我将在txt文件上执行这个脚本,该文件将包含这样的行 轻松!只需使用getElementsByClassName。例如:
document.getElementsByClassName('Key')[0].value
或者,如果必须按id获取值,则可以使用getElementById
document.getElementById('idHere').value
var inputs=getElementsByClassName('Key');
对于(var i=0;i检查此项
window.onload=function(){
var hidden=document.getElementsByClassName(“Key”);
警报(隐藏[0]。值);
}
这里有4种方法可以获取.Key
的值。另外,我在jQuery中添加了一种更好的方法,使用方法val()
一小条
var k=document.querySelector('.Key').value;
控制台日志(k);
//如果。钥匙在
var e=document.forms[0]。元素[0]。值;
控制台日志(e);
var y=document.getElementsByTagName('input')[0]。值;
控制台日志(y);
var s=document.getElementsByClassName('Key')[0]。值;
控制台日志;
//顺便说一句,有一种更好的方法可以使用jQuery查找值
var$Key=$('.Key').val();
console.log($Key);
谢谢大家。我按如下方式解决此问题:
var regEx = /class="Name"+ value="(.*?)"/;
newName = result.match(regEx)[1];
var regEx2 = /class="Key"+ value="(.*?)"/;
var key = result.match(regEx2)[1];
Alert(key + ' ' + newName );
在txt文件上不起作用是什么意思?是否要读取文本文件并找到包含
的行?您没有说得很清楚。@Pino77getElementsByClassName返回一个HTML元素数组,因此该行不起作用。@James111是的,没错!当然可以[0]
引用HTMLCollection中的第一项。请尝试document.querySelector('.key').textContent;
var regEx = /class="Name"+ value="(.*?)"/;
newName = result.match(regEx)[1];
var regEx2 = /class="Key"+ value="(.*?)"/;
var key = result.match(regEx2)[1];
Alert(key + ' ' + newName );