简体   繁体   English

从输入框,Javascript,getElementById获取价值

[英]Getting value from a Input Box , Javascript, getElementById

SO i am trying to get the value or the contents of an HTML text box. 所以我正在尝试获取HTML文本框的值或内容。 I have tried various types of methods but none of them work. 我尝试了各种类型的方法,但没有一个起作用。 According to the debugger , the code stops at getelementbyid method. 根据调试器,代码在getelementbyid方法处停止。 The commented lines are the methods that I have already tried. 注释行是我已经尝试过的方法。 Some of them return null while some of them return NaN and most of them just return a blank page. 其中一些返回null,而有些返回NaN,而大多数返回空白页。 help is much appreciated. 非常感谢帮助。

<html>
<head>
<script type="text/javascript" >
function calculateit(){
document.open();
var number = document.getElementsByName('xyz')[0].value

//var number = document.getElementsByName("xyz").value;
//var number = document.getElementsByName('xyz').value;
//var number = document.getElementsByName("xyz");
//var number = document.getElementsByName('xyz');

//var number = document.getElementsById("xyz").value;
//var number = document.getElementsById('xyz').value;
//var number = document.getElementsById("xyz");
//var number = document.getElementsById('xyz');

//var number = document.form1.xyz.value;  //form 1 was my form name and/or id 


document.writeln(number);

var newtemp = 0;

var newtemp = tempera *9/5+32;

document.write(newtemp);
}

</script>

</head>

<body>
<input type="text" id="xyz" name="xyz">
<button title="calculate" onclick="calculateit()">calculate </button>


</body>
</html>

You're using the wrong method. 您使用了错误的方法。 The two widely supported methods for javascript are "getElementsByTagName" and "getElementById". javascript广泛支持的两种方法是“ getElementsByTagName”和“ getElementById”。 Note exactly how "getElementById" is spelled. 请确切注意“ getElementById”的拼写方式。 It is meant to get one element with the exact id that you specify. 它旨在获得一个具有您指定的确切ID的元素。 "getElementsByTagName" gets all elements of a certain tag...such as "div". “ getElementsByTagName”获取特定标签的所有元素,例如“ div”。 When using "getElementById", you don't to index it or anything - it either returns null (can't find) or the exact element reference. 使用“ getElementById”时,您不对其进行索引或任何其他操作-它返回null(找不到)或确切的元素引用。 From there, since it is a textarea, you can use ".value" to get the current value, like you already are. 从那里开始,因为它是一个文本区域,所以您可以像现在一样使用“ .value”获取当前值。

ALSO: 也:

You probably shouldn't use "document.write" or any of its related write methods AFTER the page has been rendered. 呈现页面后,您可能不应该使用“ document.write”或其任何相关的写入方法。 In your example, that's exactly what you're doing, so once you get the ".value" stuff working, I would change that. 在您的示例中,这就是您正在执行的操作,因此一旦使“ .value”工作正常,我将对其进行更改。 The point is that "document.write" is more for during page rendering...so if you had javascript inline with the HTML body or something. 关键是“ document.write”更多用于页面渲染期间...因此,如果您的JavaScript内联于HTML正文或​​其他内容。 Something like: 就像是:

<html>
<head>

</head>
<body>
    <script type="text/javascript">
        document.write("testing");
    </script>
</body>
</html>

would be fine, but still not preferred. 会很好,但仍然不是首选。 The fact that you have it in a function, that is called on a button click, means it is not during page render, and shouldn't be used. 您将其包含在函数中(即单击按钮时)的事实意味着它不在页面渲染期间,因此不应使用。 A more practical approach is to have a <div> on the page and add text to it when necessary, using something like ".innerHTML". 一种更实用的方法是在页面上放置<div> ,并在必要时使用诸如“ .innerHTML”之类的内容添加文本。 That way, things are dynamic and not overwritten in the actual document. 这样,事情是动态的,不会在实际文档中覆盖。

它是getElementById,而不是getElementsById。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM