简体   繁体   English

简单的 JavaScript 函数无法正常工作

[英]Simple JavaScript Function Doesn't Work With No Errors

I'm new to js and my first and simplest function is not working with no errors我是 js 新手,我的第一个也是最简单的功能无法正常工作

Other js codes work fine but not this其他js代码工作正常,但不是这个

Here is the code: I'm expecting it to simply print what i write in the input field这是代码:我希望它简单地打印我在输入字段中写的内容

HTML file: HTML文件:

<html>
<head></head>

 <body>

<input type="text" id="myid">
<button onclick="myfunc()">click</button>

<script src="javascript.js"></script>

</body>
</html>

javascript.js file: javascript.js 文件:

/*global document */

function myfunc() {
    "use strict";
  document.getElementById("myid").value;
}

If you want to print the value of the input field, you can use console.log() to print it's contents to the console.如果要打印输入字段的值,可以使用console.log()将其内容打印到控制台。 Save the value of the input in a variable for example.例如,将输入的值保存在变量中。 Then you can log the value of this variable in your browser's console, or use it for another purpose.然后,您可以在浏览器的控制台中记录此变量的值,或将其用于其他目的。

function myfunc() {
     "use strict";
     var value = document.getElementById("myid").value;
     console.log(value)
}

document.getElementById("myid").value did only get the input's value. document.getElementById("myid").value只获取输入的值。 But you didn't do anything with it.但你没有对它做任何事情。

You can also directly log the value without saving it to a variable first, see the example below.您也可以直接记录该值,而无需先将其保存到变量中,请参见下面的示例。

function myfunc() {
     "use strict";
     console.log(document.getElementById("myid").value)
}

To show the value on the page, you can create a empty placeholder with an ID you can target.要在页面上显示值,您可以创建一个空的占位符,其中包含您可以定位的 ID。 Then set the textContent of this placeholder to the value of the input.然后将此占位符的textContent设置为输入的值。 See the example below.请参阅下面的示例。

<html>
    <head></head>

    <body>

        <input type="text" id="myid">
        <button onclick="myfunc()">click</button>
        <!-- This is the output placeholder -->
        <p id="output"></p>

        <script src="javascript.js"></script>

   </body>
</html>

function myfunc() {
     "use strict";
     var value = document.getElementById("myid").value;
     /* Select the output placeholder */
     var outputElement = document.getElementById("output");
     /* Set the input fields value as the text content */
     outputElement.textContent = value;
}

If you want that to be visible in page, Create a div in HTML and set a id to it.如果您希望它在页面中可见,请在 HTML 中创建一个 div 并为其设置一个 id。 After that in JS get the value in a variable之后在 JS 中获取变量中的值

var myId=document.getElementById("myid").value; and set that in new div id.并将其设置在新的 div id 中。

document.getElementById("printId").innerHTML="myId";

You don't do anything with the value, you just retrieve it and throw it away.你不会这个值任何事情,你只是取回它然后扔掉它。 Try尝试

function myfunc() {
  alert(document.getElementById("myid").value);
}

It's a simple case of this function doesn't do anything.这是一个简单的例子,这个函数不做任何事情。

Your code is correct in terms of getting the value of the input field with the id myid .您的代码在获取具有 id myid的输入字段的值方面是正确的。

But after that you're not doing anything with it, if you want to see it you do console.log() on the value, or you can assign it to a variable to work with it later on const inputValue = document.getElementById("myid").value;但是在那之后你什么都不用做,如果你想看到它,你可以对值执行console.log() ,或者你可以将它分配给一个变量以便稍后在const inputValue = document.getElementById("myid").value;上使用它const inputValue = document.getElementById("myid").value;

The line document.getElementById("myid").value;document.getElementById("myid").value; simply just retrieves the value, and doesn't do anything with it, just throws it away, which is why you don't see any errors or feedback.只是简单地检索该值,并且不对它做任何事情,只是将其扔掉,这就是为什么您看不到任何错误或反馈的原因。

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

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