简体   繁体   English

JS函数不显示任何内容

[英]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: 您有一些错误:

  1. .value() is not a function: use .value .value()不是函数:请使用.value
  2. You don't need that semicolon at the end of the if statement if语句的末尾不需要分号
  3. Your text box might not have the id that you're referring to: make sure that you have id="myTextBox" in the opening tag 您的文本框可能没有您要引用的id :确保在开始标记中有id="myTextBox"
  4. While it won't cause any errors, your function definition lists a parameter, but you do not pass one when you call it. 虽然它不会引起任何错误,但是您的函数定义列出了一个参数,但是在调用它时您不会传递任何参数。

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

Demo 演示版

value is not a function, it's a property, therefore: value不是函数,而是属性,因此:

var myVal=document.getElementById('myTextBox').value;

As you're brand new to JS, I'll give you a piece of advice: If you want to manipulate the DOM, use jQuery . 当您是JS的新手时,我会给您一些建议:如果您想操纵DOM,请使用jQuery

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>

Fiddle demo 小提琴演示

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

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