Html Javascript,查看 [object HTMLInputElement]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15383765/
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
Javascript, viewing [object HTMLInputElement]
提问by iPhoneDeveloper
I'm just started to learn HTML. Doing an alert()
on one of my variables gives me this result [object HTMLInputElement]
.
我刚开始学习 HTML。在alert()
我的一个变量上做一个给我这个结果[object HTMLInputElement]
。
How to get the data, that were added in text field, where my input type is text?
如何获取在文本字段中添加的数据,其中我的输入类型是文本?
采纳答案by simonzack
Say your variable is myNode
, you can do myNode.value
to retrieve the value of input elements.
假设您的变量是myNode
,您可以myNode.value
检索输入元素的值。
Firebug has a "DOM" tab which shows useful DOM attributes.
Firebug 有一个“DOM”选项卡,显示有用的 DOM 属性。
Also see the mozilla page for a reference: https://developer.mozilla.org/en-US/docs/DOM/HTMLInputElement
另请参阅 mozilla 页面以获取参考:https: //developer.mozilla.org/en-US/docs/DOM/HTMLInputElement
回答by Xavi López
If the element is an <input type="text">
, you should query the value
attribute:
如果元素是<input type="text">
,则应查询value
属性:
alert(element.value);
See an example in this jsFiddle.
请参阅此jsFiddle 中的示例。
Also, and seeing you're starting to learn HTML, you might consider using console.log()
instead of alert()
for debugging purposes. It doesn't interrupt the execution flow of the script, and you can have a general view of all logs in almost every browser with developer tools (except that one, obviously).
此外,看到您开始学习 HTML,您可能会考虑使用console.log()
而不是alert()
用于调试目的。它不会中断脚本的执行流程,并且您可以使用开发人员工具(显然除了那个)对几乎每个浏览器中的所有日志都有一个总体视图。
And of course, you could consider using a web development tool like Firebug, for instance, which is a powerful addon for Firefox that provides a lot of functionalities (debugging javascript code, DOM inspector, real-time DOM/CSS changes, request monitoring ...)
当然,您可以考虑使用像Firebug这样的 Web 开发工具,例如,它是一个强大的 Firefox 插件,提供了许多功能(调试 javascript 代码、DOM 检查器、实时 DOM/CSS 更改、请求监控)。 ..)
回答by Back Door
change:
改变:
$("input:text").change(function() {
var value=$("input:text").val();
alert(value);
});
to
到
$("input:text").change(function() {
var value=$("input[type=text].selector").val();
alert(value);
});
note:selector:id,class..
注意:选择器:id,类..
回答by Alex.K.
<input type="text" />
<script>
$("input:text").change(function() {
var value=$("input:text").val();
alert(value);
});
</script>
use .val() to get value of the element (jquery method), $("input:text") this selector to select your input, .change() to bind an event handler to the "change" JavaScript event.
使用 .val() 获取元素的值(jquery 方法),使用 $("input:text") 此选择器选择您的输入,使用 .change() 将事件处理程序绑定到“change”JavaScript 事件。
回答by Harsh Aryan
It's not because you are using alert, it will happen when use document.write() too. This problem generally arises when you name your id or class of any tag as same as any variable which you are using in you javascript code. Try by changing either the javascript variable name or by changing your tag's id/class name.
这不是因为您正在使用警报,它也会在使用 document.write() 时发生。当您将任何标签的 id 或类命名为与您在 JavaScript 代码中使用的任何变量相同时,通常会出现此问题。尝试更改 javascript 变量名称或更改标签的 ID/类名称。
My code example:bank.html
我的代码示例:bank.html
<!doctype html>
<html>
<head>
<title>Transaction Tracker</title>
<script src="bank.js"></script>
</head>
<body>
<div><button onclick="bitch()">Press me!</button></div>
</body>
</html>
Javascript code:bank.js
JavaScript 代码:bank.js
function bitch(){ amt = 0;
var a = Math.random(); ran = Math.floor(a * 100);
return ran; }
function all(){
amt = amt + bitch(); document.write(amt + "
"); } setInterval(all,2000);
you can have a look and understand the concept from my code. Here i have used a variable named 'amt' in JS. You just try to run my code. It will work fine but as you put an [id="amt"](without square brackets) (which is a variable name in JS code )for div tag in body of html you will see the same error that you are talking about. So simple solution is to change either the variable name or the id or class name.
您可以从我的代码中查看并理解这个概念。在这里,我在 JS 中使用了一个名为“amt”的变量。你只是试着运行我的代码。它会正常工作,但是当您为 html 正文中的 div 标签放置一个 [id="amt"](不带方括号)(这是 JS 代码中的变量名称)时,您将看到与您正在谈论的相同的错误。如此简单的解决方案是更改变量名或 id 或类名。
回答by erevos13
When you get a value from client make and that a value for example.
当你从客户那里得到一个值时,例如一个值。
var current_text = document.getElementById('user_text').value;
var http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200 ){
var response = http.responseText;
document.getElementById('server_response').value = response;
console.log(response.value);
}