2014-02-09 57 views
-3

我真的不知道为什么这两个函数 - 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

在此先感谢

+2

邮政编码**在问题本身**,不要只是链接。这是**为什么**系统阻止您发布jsfiddle链接,直到您将其标记为代码。当然,围绕这样的系统工作并不是你最好的选择? –

+0

[jsFiddle:html和js之间没有连接?无法从按钮调用简单的函数?](http://stackoverflow.com/questions/14499783/jsfiddle-no-connection-between-html-and-js-cant-call-simple-function-from-but) – Sirko

+0

TJ Crowder,对不起,我不知道! 但我不能再问任何问题吗? –

回答

0

首先do是一个关键字,所以你不能用它作为方法的名字 - 它像check

重命名为

第二个内联事件管理器的方法必须在全局范围内 - 在小提琴左侧面板的第二个下拉列表中选择主体/头部

演示:Fiddle

0

do是一个保留关键字。您不能将其用作函数名称。将它重命名为其他内容。其次,必须在全局范围内定义内联事件处理程序。在你的小提琴,你必须选择在头选项

裹,=是赋值运算符,用来比较符合使用=====,错误(y.value = "enter your name here")

使用

function do1() 

DEMO

+0

大声笑,是的,我忘了! ..对不起你的时间和谢谢:) –

+0

@OmarAhmed,很高兴我能帮上忙。我希望我覆盖所有基地 – Satpal

+0

哦,是的..非常感谢 –

0

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

1

你有3个问题:

1 - 这是你的jsfiddle选项您选择包装所有的代码在onLoad,所以功能都没有在全球范围内,您可以修复它我在下面的代码中。

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

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

改变它

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

3-另一万阿英,蒋达清是do是一个保留字,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();     
    } 
} 
+0

如果(y.value =“在这里输入你的名字”)' – Satpal

+1

@Satpal:感谢指出,我更新了我的答案。 –

相关问题