javascript 如何根据元素的ID打印console.log中的值?

flvtvl50  于 2023-02-15  发布在  Java
关注(0)|答案(9)|浏览(205)
<input id="email" name="email" type="text" value="ooooo@gmail.com">

如何获取id=email的值并将其打印到我的控制台或任何地方进行测试?
我试过了,但没有运气:(

script type="text/javascript">
    document.getElementById('email').innerText;
</script>
a6b3iqyw

a6b3iqyw1#

有两种类型的元素
1.块-可以在jquery中使用-.innerHTML.html()访问这些块,如div的span等
1.非块元素-这些元素可以通过jquery中的.value.val()访问,如输入类型等
你要做的就是

console.log($('#email').val())  //jquery

or 

console.log(document.getElementById('email').value); // javascript
hec6srdp

hec6srdp2#

尝试

console.log($('#email').val());
x7rlezfr

x7rlezfr3#

尝试提醒...
http://jsfiddle.net/nemb666L/

var email = $("#email").val();
alert(email);
1l5u6lss

1l5u6lss4#

这将把整个对象“打印”到控制台

console.log("input[id='email'] - %o", document.getElementById('email'));
ht4b089n

ht4b089n5#

不使用jQuery:

console.log(document.getElementById('email').value);
jqjz2hbq

jqjz2hbq6#

很管用

console.log(email.value)
vd8tlhqk

vd8tlhqk7#

在Chrome开发工具控制台中删除.value:

document.getElementById('#id')
ntjbwcob

ntjbwcob8#

let inputId = document.querySelector("#email");
console.log(inputId);
ercv8c1e

ercv8c1e9#

如果没有定义email(您的id),只需:

email.innerHTML

相关问题