如何通过 JavaScript 获取 HTML 元素值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42573637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:16:13  来源:igfitidea点击:

How can I get HTML element value by JavaScript?

javascripthtml

提问by the pheonix

<div class="gallery_re_form">
  <form action="http://gall.dcinside.com/?/forms/comment_submit" method="post" accept-charset="utf-8" name="comment_write" id="comment_write" />
      <div style="display:none">
          <input type="hidden" name="ci_t" value="625752bb366f010c56e506e5d0f93822" />
      </div>
      <input type="hidden" name="ehqo_C" id="ehqo_C" value="spam_key" />
      <input type="hidden" name="spam_key" id="spam_key" value="rhkdrhgkwlak!!" />
      <input type="hidden" name="fb8f95190d2b70480acb309f91653d7e4416d4f99a0b67a0b866f06087fdec4588f68fe210675e74919d2ecc50e08f1fd6f75119" id="fb8f95190d2b70480acb309f91653d7e4416d4f99a0b67a0b866f06087fdec4588f68fe210675e74919d2ecc50e08f1fd6f75119" value="b9d3cc45576e30144d8f299fdc61356552989b3aba08238c811b1d3183a104cc2d1d3984af72c4f2eaa2738ca41819d05533731a" />
      <input type="hidden" name="service_code" value="21ac6e96ad152e8f15a05b7350a24759b5606fa191c17e042e4d0175735f4c61d63153c3ce7b4eb89b565a7f6a04ad0667df75f39625ab6fe0816d23f4bed5387546b52d6874b5c201e54df7b5db9187219b048cffc9ef8a106febabfba125eff122df732d9cc52fcaae8b11c42ff5f6fd5ef81901df4103827e7233615992141c180be76852634d53b60c6f911ccdfc762b3db554d8ee36528547bceede30697120c5291acb255a" />
      <br />
      <div class="re_input">
          <div>
              <ul>
                  <li>
                      <input type="text" name="name" id="name" class="re_name re_in" onfocus="this.style.background='#FFFFFF'" title="nickname" />
                  </li>
                  <li>
                      <input type="password" name="password" id="password" class="re_pw re_in" onfocus="this.style.background='#FFFFFF'" title="password" />
                  </li>
              </ul>
          </div>
      </div>
  </form>

I'm trying to rewrite google chrome extensions. I want to get #spam_keys #service_codes value in this HTML response.

我正在尝试重写 google chrome 扩展。我想在此 HTML 响应中获取#spam_keys#service_code值。

I have not used JavaScript.

我没有使用过 JavaScript。

How can I get HTML element id (spam_key, service_code) and value by JavaScript?

如何通过 JavaScript 获取 HTML 元素 ID(spam_key、service_code)和值?

采纳答案by Mg Thar

You want to retrieve the spam_key element and service_code element's values?

您想检索 spam_key 元素和 service_code 元素的值吗?

var spam_key = document.querySelector('#spam_key');
var service_code = document.querySelector('input[name=service_code]');

console.log(spam_key.value);
console.log(service_code.value);

You can learn more about selection element from javascript at here

您可以在此处从 javascript 中了解有关选择元素的更多信息

回答by u7621635

DOM has a documentobject , you can use it like this to get the HTML element what you want

DOM 有一个document对象,你可以像这样使用它来获取你想要的 HTML 元素

document.getElementById("id")

Now to get value, just use valueproperty of this element.

现在要获取值,只需使用value此元素的属性。

document.getElementById("id").value

回答by Yogesh Mistry

You can get value of an HTML input element using:

您可以使用以下方法获取 HTML 输入元素的值:

var spam_key = document.getElementById('spam_key').value;
var service_code = document.getElementById('service_code').value;

回答by Ashutosh Jha

Try this, It should give the value of spam_key

试试这个,它应该给出 spam_key 的值

document.getElementById('spam_key').value;

回答by Epsilon47

If you have jQuery included you can get it by following: $("#spam_key").val()

如果您包含 jQuery,您可以通过以下方式获取它: $("#spam_key").val()