英文:
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
<input type="text" class="search-bar" placeholder="Search">
<button onclick="show()">Show Your Text</button>
<script>
function show() {
var x = document.getElementsByClassName("search-bar")[0].value;
alert(x);
}
</script>
答案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.
<input type="text" class="search-bar" placeholder="Search">
<button onclick="show()">Show Your Text</button>
<script>
function show() {
var x = document.getElementsByClassName("search-bar")[0].value;
alert(x);
}
</script>
答案3
得分: 0
你需要完成两件事情才能完成这个任务:
- 将js代码放入
<script>
标签中。 - 将下面的代码放入
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:
- Put js code into
<script>
tag. - Put this below code into
show()
function so that the alert text will display dynamically when clicking the button.
var x = document.getElementsByClassName("search-bar")[0].value;
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<input type="text" class="search-bar" placeholder="Search">
<button onclick="show()">Show Your Text</button>
<script>
function show(){
var x = document.getElementsByClassName("search-bar")[0].value;
alert(x);
}
</script>
<!-- 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.
<input type="text" class="search-bar" placeholder="Search">
<button onclick="show()">Show Your Text</button>
<script>
function show(){
var x = document.getElementsByClassName("search-bar")[0].value;
alert(x);
}
problem
</script>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论