为什么值在这里使用警告功能时没有显示?

huangapple go评论74阅读模式
英文:

why the value is not shown here using alert function

问题

<input type="text" class="search-bar" placeholder="Search">
<button onclick="show()">Show Your Text</button>
var x = document.getElementsByClassName("search-bar")[0].value;
function show(){
alert(x);
}

问题
在alert函数中没有显示我在文本框中写的任何内容

我在文本框中写的值会显示在警报框函数中
英文:
<input type="text" class="search-bar" placeholder="Search">
<button onclick="show()">Show Your Text</button>
var x = document.getElementsByClassName("search-bar")[0].value;
function show(){
alert(x);
}

problem
in alert function the not show anything which i write in text box

The value which i write in textbox is show in alert box function

答案1

得分: 3

尝试这个

<input type="text" class="search-bar" placeholder="搜索">
<button onclick="show()">显示您的文本</button>

<script>
function show() {
  var x = document.getElementsByClassName("search-bar")[0].value;
  alert(x);
}
</script>
英文:

Try this

&lt;input type=&quot;text&quot; class=&quot;search-bar&quot; placeholder=&quot;Search&quot;&gt;
&lt;button onclick=&quot;show()&quot;&gt;Show Your Text&lt;/button&gt;

&lt;script&gt;
function show() {
  var x = document.getElementsByClassName(&quot;search-bar&quot;)[0].value;
  alert(x);
}
&lt;/script&gt;

答案2

得分: 0

你需要在show()函数内检索输入字段的值。

<input type="text" class="search-bar" placeholder="搜索">
<button onclick="show()">显示您的文本</button>

<script>
function show() {
  var x = document.getElementsByClassName("search-bar")[0].value;
  alert(x);
}
</script>
英文:

you need to retrieve the value of the input field inside the show() function.

&lt;input type=&quot;text&quot; class=&quot;search-bar&quot; placeholder=&quot;Search&quot;&gt;
&lt;button onclick=&quot;show()&quot;&gt;Show Your Text&lt;/button&gt;

&lt;script&gt;
function show() {
  var x = document.getElementsByClassName(&quot;search-bar&quot;)[0].value;
  alert(x);
}
&lt;/script&gt;

答案3

得分: 0

你需要完成两件事情才能完成这个任务:

  1. 将js代码放入<script>标签中。
  2. 将下面的代码放入show()函数中,这样在点击按钮时,警告文本将动态显示。
var x = document.getElementsByClassName("search-bar")[0].value;
<input type="text" class="search-bar" placeholder="搜索">
<button onclick="show()">显示您的文本</button>
<script>
  function show(){
    var x = document.getElementsByClassName("search-bar")[0].value;
    alert(x);
  }
</script>
英文:

You need to do two things to get this done:

  1. Put js code into &lt;script&gt; tag.
  2. Put this below code into show() function so that the alert text will display dynamically when clicking the button.
var x = document.getElementsByClassName(&quot;search-bar&quot;)[0].value;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; class=&quot;search-bar&quot; placeholder=&quot;Search&quot;&gt;
&lt;button onclick=&quot;show()&quot;&gt;Show Your Text&lt;/button&gt;
&lt;script&gt;
  function show(){
    var x = document.getElementsByClassName(&quot;search-bar&quot;)[0].value;
    alert(x);
  }
&lt;/script&gt;

<!-- end snippet -->

答案4

得分: 0

你在函数执行之前赋值,这就是为什么它总是空白的。

<input type="text" class="search-bar" placeholder="搜索">
<button onclick="显示()">显示你的文本</button>

<script>
    function 显示(){
        var x = document.getElementsByClassName("search-bar")[0].value;
        alert(x);
    }
</script>
英文:

You are assigning the value before the function is executed, that's why it's always blank.

&lt;input type=&quot;text&quot; class=&quot;search-bar&quot; placeholder=&quot;Search&quot;&gt;
&lt;button onclick=&quot;show()&quot;&gt;Show Your Text&lt;/button&gt;

&lt;script&gt;
    
    function show(){
        var x = document.getElementsByClassName(&quot;search-bar&quot;)[0].value;
    alert(x);
    }
    problem
&lt;/script&gt;

答案5

得分: 0

尝试在函数内部仅声明变量 "x",因为当单击按钮时,会触发 onClick 事件并运行显示按钮,在显示函数内部此时我们有一个未定义的值。

英文:

try to declare variable "x" in the function only, because when you click on the button onClick event gets triggered and runs the show button, and inside the show function at that moment we have an undefined value.

huangapple
  • 本文由 发表于 2023年6月22日 14:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定