【问题标题】:why these two functions don't want to run? [duplicate]为什么这两个功能不想运行? [复制]
【发布时间】:2014-02-09 11:36:19
【问题描述】:

我真的不知道为什么这两个函数 - leave() & do() - 不运行!

    function leave() 
    {
        var x = document.getElementById("x");
        if(x.value == "")
        {
            alert("please enter your name");
            x.focus();
        }
    }

    function do()
    {
        var y = document.getElementById("y");
        if (y.value = "enter your name here")
        {
            alert("enter your last name");
            y.focus();
            y.select();                
        }
    }

这是我的代码:http://jsfiddle.net/BsHa2

提前致谢

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

你有 3 个问题:

1- 在您的 jsfiddle 选项中,您选择将所有代码包装在 onLoad 中,因此这些函数不在全局上下文中,您可以像我在下面的代码中那样修复它。

2- 此行会将值设置为 y 输入的值:

if (y.value = "enter your name here")

改成

if (y.value == "enter your name here")

3- 另一个问题是do 是保留字,请勿使用 保留字,尽管它会在某些浏览器中执行您想要的操作。

window.leave = function leave() 
{
    var x = document.getElementById("x");
    if(x.value == "")
    {
        alert("please enter your name");
        x.focus();
    }
}

window.check = function check()
{
    var y = document.getElementById("y");
    if (y.value = "enter your name here")
    {
        alert("enter your last name");
        y.focus();
        y.select();                
    }
}

【讨论】:

  • if (y.value = "enter your name here")怎么样
  • @Satpal:感谢您指出这一点,我更新了我的答案。
【解决方案2】:

第一个 do 是一个关键字,因此您不能将其用作方法名称 - 将其重命名为 check 之类的名称

第二个内联事件处理方法必须在全局范围内 - 在小提琴左侧面板的第二个下拉列表中选择 body/head

演示:Fiddle

【讨论】:

    【解决方案3】:

    do 是保留关键字。您不能将其用作函数名称。将其重命名为其他名称。

    其次,必须在全局范围内定义内联事件处理程序。在您的小提琴中,您必须选择 Wrap in head 选项

    第三,=是赋值运算符,比较使用==和===,错误在(y.value = "enter your name here")行

    使用

    function do1()
    

    DEMO

    【讨论】:

    • 大声笑,是的,我忘记了! .. 对不起你的时间,无论如何谢谢:)
    • @OmarAhmed,很高兴我能帮上忙。我希望我涵盖了所有基础
    • 哦,是的..非常感谢
    【解决方案4】:

    do 是保留关键字。您不能将其用作函数名称。

    另外,你这里有一个错误:

    if (y.value = "enter your name here")
    

    你需要检查是否相等:

    if (y.value === "enter your name here")
    

    顺便说一句,您应该真正考虑为变量赋予有意义的名称并使用不显眼的事件处理程序:

    <form id="myForm">
        <label for="firstName">First Name:</label>
        <input type="text" name="input" id="firstName" size="20">
        <br/>
        <label for="lastName">Last Name:</label>
        <input type="text" id="lastName" size="20" value="enter your name here">
        <input type="button" id="check" value="Check!">
    </form>
    
    var firstName = document.getElementById("firstName"),
        lastName = document.getElementById("lastName"),
        checkButton = document.getElementById("check");
    
    firstName.onblur = function(){
      if (this.value === ""){
        alert("please enter your name");
        this.focus();
      }  
    }
    
    check.onclick = function(e){
        e.preventDefault();
        if (lastName.value === "enter your name here") {
          alert("enter your last name");
          lastName.focus();
        }
    }
    

    fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-05
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      相关资源
      最近更新 更多