简体   繁体   English

我不确定为什么我的onclick按钮无法与我在文本框中输入的内容链接起来?

[英]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.

相关问题 为什么即使我在模式弹出窗口中添加了 Onclick 事件,我的 Asp 按钮也不起作用? - Why my Asp button doesn't works even i added Onclick Event inside modal popup? 为什么我的指令没有显示我要传递给它的字符串 - Why my directive doesn't show a String I'm passing to it 如何将我的 HTML 按钮链接到 JS onclick - How do I link my HTML button to a JS onclick 为什么在OnclientClickEvent之后无法执行onClick? - Why can't I execute my onClick after my OnclientClickEvent? 我不确定为什么我的 for 循环不继续增加“i” - I'm not sure why my for loop doesn't continue incrementing "i" 为什么当我调用具有返回值的函数时,我的onclick事件不起作用? - Why when i call a function with returning value, my onclick event doesn't work? 我的onclick事件是在页面加载时执行的,而不是在单击按钮时执行的,我不知道为什么 - My onclick event performs when the page loads instead of when the button is clicked, and i don't know why 为什么按下按钮时我的onclick功能没有激活? - Why isn't my onclick function activating when I press the button? 我不知道为什么当我在我的 HTML 程序上单击“Click = Past”按钮时,绿色字符图像不显示我在可汗学院尝试过 - I don't know why When I click the button "Click = Past" on my HTML program the green character image doesn't show up I tried to this in Khan academy 为什么单击“新产品”按钮后,我的“保存”按钮和“取消”按钮没有显示? - Why my Save button and Cancel Button doesn't show after I clicked the New Product button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM