[英]JS function not displaying anything
I have made a function and captured the value of a textbox. 我做了一个函数,并捕获了文本框的值。 On a button click, it should alert the value. 单击按钮时,它应该警告该值。 The code is: 代码是:
function substitute (argument) {
var myVal=document.getElementById('myTextBox').value();
alert(myVal);
if (myVal.length==0) {
alert('Empty Textbox');
};
}
It is then executed using this: 然后使用以下命令执行:
<input type="button" name="Button" value="Click Me" onclick="substitute();">
But nothing happens. 但是什么也没发生。 Please tell me where am I doing it wrong. 请告诉我我在哪里做错了。
You have a few errors: 您有一些错误:
.value()
is not a function: use .value
.value()
不是函数:请使用.value
if
statement 在if
语句的末尾不需要分号 id
that you're referring to: make sure that you have id="myTextBox"
in the opening tag 您的文本框可能没有您要引用的id
:确保在开始标记中有id="myTextBox"
Always check your browser console for errors if nothing is happening. 如果没有任何反应,请始终检查浏览器控制台是否有错误。 Always. 总是。
function substitute () { // Parameter "argument" is not necessary
var myVal=document.getElementById('myTextBox').value; //.value, not .value()
alert(myVal);
if (myVal.length==0) {
alert('Empty Textbox');
}; // You don't need this semicolon
} // You were missing this curly brace
In Your code, you are not passing any parameter to function subsititute(), But @ function definition you wrote substitute(argument). 在您的代码中,您没有将任何参数传递给函数subsititute(),而是在@函数定义中编写了replace(argument)。 Thats a big issue, and also you put there is a semi-column after if condition inside the function. 那是一个大问题,并且您在函数内部的if条件之后还放置了一个半列。 No need of it 不用了
Try This.. 尝试这个..
<input type="text" id="myText" value="Hello" />
<input type="button" name="Button" value="Click Me" onclick="substitute();" />
In script part, 在脚本部分,
function subsitute()
{
var myVal=$('#myText').val();
alert(myVal);
if (myVal=='')
{
alert('Empty Textbox');
}
}
http://jsbin.com/tewudopi/1/edit http://jsbin.com/tewudopi/1/edit
Add an ID to the button. 向按钮添加一个ID。 When referencing the button, it is not ".value()", it is simply ".value". 引用按钮时,它不是“ .value()”,而是“ .value”。
As a side note, you should use === instead of == to compare. 附带说明,您应该使用===而不是==进行比较。
function substitute() {
var myVal=document.getElementById('myTextBox');
alert(myVal.value);
if (myVal.value.length===0) {
alert('Empty Textbox');
}
}
Maybe something like the following. 也许像下面这样。
<script type="text/javascript">
function substitute() {
var myVal=document.getElementById('myTextBox').value;
if (myVal.length>0) {
alert(myVal);
}
if (myVal.length==0) {
alert('Empty Textbox');
}
}
</script>
<input type="button" name="Button" value="Click Me" onclick="substitute();">
<textarea name="myTextBox" id="myTextBox"></textarea>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.