如何获取HTML标记的值(getAttribute返回NULL)

时间:2017-03-22 13:55:08

标签: javascript html forms dataset getattribute

我想获取所有字段的value-tag的值。

e.g。我想得到:

  • / wEPDwUKLTUwMTUxNzcwOGRklQy3YSnUhKEGDrFOOy2aa5Lg4qY =
  • 83BB589F - / wEWBwKN / dHeCAKl1bKzCQK1qbSRCwKpiJ / dDgKE8 / 3 / CQKM + 5aFCQKM54rGBs1XK43QekWbnANIKpGR9 + Q85Kji
  • 0151CSE003
  • @Abhishek

我的HTML代码,目前正在提供NULLUndefined

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<body>

 <form name="form1" method="post" action="http://abc/Home/default.aspx" id="form1">

    <div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTUwMTUxNzcwOGRklQy3YSnUhKEGDrFOOy2aa5Lg4qY="></div>
    <div><input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="83BB589F"></div>
    <div><input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBwKN/dHeCAKl1bKzCQK1qbSRCwKpiJ/dDgKE8/3/CQKM+5aFCQKM54rGBs1XK43QekWbnANIKpGR9+Q85Kji"></div>
    User Name<input name="txtUserName" type="text" id="txtUserName" value="0151CSE003">
    Password<input name="txtPassword" type="password" id="txtPassword"  value="@Abhishek">
    <input type="submit" name="Ulogin" value="Login"  id="Ulogin" >

</form>
<br><br>
<div id="op"></div>

<script>

{
    //var x=document.getElementsByTagName("div")[0].getAttribute('value');
    var x=document.getElementsByTagName("div")[1];


    alert(x.getAttribute('data-value'));
    alert(x.dataset.value);

    document.getElementById("op").innerHTML=x;

}
</script>

</body></html>

4 个答案:

答案 0 :(得分:2)

请检查此代码,看看它是如何获取值的。你的一些代码有点不对。

var viewState = document.querySelector("#__VIEWSTATE");
var viewStateGenerator = document.querySelector("#__VIEWSTATEGENERATOR");

console.log(viewState.value);
console.log(viewStateGenerator.value);

document.querySelector('#op').innerHTML = viewState.value;

答案 1 :(得分:0)

应该是

 x.getAttribute('value')

您不会追溯添加“data-”标记,而是必须包含它。

答案 2 :(得分:0)

好的,你的JavaScript有点乱。以下代码应符合您的要求。将Value属性与getElementById

一起使用
var VIEWSTATE = document.getElementById("__VIEWSTATE").value;
  var VIEWSTATEGENERATOR = document.getElementById("__VIEWSTATEGENERATOR").value;
  var EVENTVALIDATION = document.getElementById("__EVENTVALIDATION").value;
  var UserName = document.getElementById("txtUserName").value;
  var Password = document.getElementById("txtPassword").value;

alert(VIEWSTATE +"\n" + VIEWSTATEGENERATOR +"\n" +  EVENTVALIDATION + "\n" + UserName+"\n"+Password);

我个人建议您使用jquery - 它更容易使用,使用更少的代码行并且功能强大。

以下是代码:

var VIEWSTATE = $("#__VIEWSTATE").val();
  var VIEWSTATEGENERATOR = $("#__VIEWSTATEGENERATOR").val();
  var EVENTVALIDATION = $("#__EVENTVALIDATION").val();
  var UserName = $("#txtUserName").val();
  var Password = $("#txtPassword").val();

alert(VIEWSTATE +"\n" + VIEWSTATEGENERATOR +"\n" +  EVENTVALIDATION + "\n" + UserName+"\n"+Password);

答案 3 :(得分:0)

如果您需要获取输入值,我可以直接进行:

    var x=document.getElementsByTagName("input");
    var showValues = '<ul>';

    for(var i=0, len=x.length; i<len; i++){
        if(x[i].id != "Ulogin"){
            showValues +='<li>'+x[i].value + '</li>';
        }
    }
    showValues += '</ul>';

    document.getElementById("op").innerHTML=showValues;

我已将其包装在ul-li标签中,如果您想将其显示给用户