[英]I'm unsure as to why my onclick button doesn't link up with what I type into my textbox?
So I am fairly new to Javascript, and I am working on some code that converts decimal numbers to binary numbers. 因此,我对Java语言还很陌生,我正在研究一些将十进制数字转换为二进制数字的代码。 However when I run this program, I can't seem to get the output I am looking for.
但是,当我运行该程序时,似乎无法获得所需的输出。 The best I have done was get my function to output an exact number already inside of the function, when I am instead trying to get an output that is generated from any number typed into the textbox?
我做的最好的事情就是让我的函数输出已经在函数内部的确切数字,而我却想获取从键入文本框的任何数字生成的输出? I'm not entirely sure where I am going wrong with this.
我不完全确定我在哪里出错。 I feel like there is something I am clearly missing, but I can't seem to grasp exactly what it is.
我觉得我显然缺少某些东西,但是我似乎无法确切地了解它到底是什么。 I've been using codecademy and w3schools to gain more knowledge of JavaScript, but if anyone has any other resources that helped them when they first started programming that would be great!
我一直在使用codecademy和w3schools来获取有关JavaScript的更多知识,但是如果任何人在他们开始编程时都拥有其他帮助他们的资源,那将是很棒的!
<!DOCTYPE html>
<html>
<body>
<p>Convert from Decimal to Binary:</p>
<form method = "post">
<p id = "demo">
<label for="decNum"></label>
<input name="decNum" type="text">
<button onclick="toBinary()">Enter</button>
</p>
</form>
<script>
function toBinary() {
document.getElementById("demo").innerHTML =
parseInt(num,10).toString(2);
}
</script>
</body>
</html>
It's because num
doesn't have a value. 这是因为
num
没有值。
Try this: 尝试这个:
<p>Convert from Decimal to Binary:</p>
<form method = "post">
<p id = "demo">
<label for="decNum"></label>
<input name="decNum" type="text" id="decNum">
<button onclick="toBinary()">Enter</button>
</p>
</form>
<script>
function toBinary() {
var num = document.getElementById("decNum").value;
document.getElementById("demo").innerHTML =
parseInt(num, 10).toString(2);
}
</script>
There are a few things I would change. 我会改变一些事情。 First off, I would keep non form elements outside of the form.
首先,我将非表单元素保留在表单之外。 The major issue is that num doesn't have a value, but to ensure it works you will also want to take the event and use event.preventDefault() to make sure it doesn't submit the form.
主要问题是num没有值,但是要确保它有效,您还需要获取事件并使用event.preventDefault()来确保它不提交表单。 Try:
尝试:
<!DOCTYPE html>
<html>
<body>
<p>Convert from Decimal to Binary:</p>
<form method="post">
<label for="decNum"></label>
<input id="field" name="decNum" type="text">
<button onclick="toBinary(event)">Enter</button>
</form>
<p id="demo"></p>
<script>
function toBinary(event) {
event.preventDefault();
var value = document.getElementById('field').value;
document.getElementById("demo").innerHTML =
parseInt(Number(value), 10).toString(2);
}
</script>
</body>
</html>
You're not defining num
. 您没有定义
num
。
Grab it from the input as such: 这样从输入中获取它:
function toBinary() { const num = document.getElementById("textInput").value; document.getElementById("demo").innerHTML = parseInt(Number(num),10).toString(2); }
<p id = "demo"></p> <label for="decNum"></label> <input name="decNum" type="text" id="textInput"> <button onclick="toBinary()">Enter</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.