2014-02-09 76 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:感謝指出,我更新了我的答案。 –

相關問題