2012-03-22 128 views
0

好的,所以我正在做一個表單驗證在這個JS中一切都很好,但是現在我在輸出中遇到問題,我試圖顯示所有選擇的數據。所以我用了action屬性,並稱爲以下功能:功能不起作用

function funcs() 
    { 
    var favor = document.reg.favor[selectedIndex].value; //Select input 
    var fname = document.reg.fname.value; // text input 
    var lname = document.reg.lname.value; // text input 
    var email = document.reg.email.value; // text input 
    var pass = document.password.value; //text input 
    for(i=0;i<document.reg.rad.length;i++) 
     { 
      if(document.reg.rad[i].checked == true) 
      { 
       var rad = document.reg.rad[i].value; // Radio input 
      } 
     } 
    if(document.reg.bike.checked == true) 
     { 
      var bike = document.reg.bike.value; //CheckBox input 
     } 
    if(document.reg.car.checked == true) 
     { 
      var car = document.reg.car.value; //CheckBox input 
     } 
    document.write('<head><link type="text/css" rel="stylesheet" href="registrationtable.css"/></head><body>'); 
    document.write("<div class = 'team'>"); 
    document.write('<table>'); 
    document.write("<tr><td> שם פרטי: </td><td>" + fname + "</td></tr> <tr><td> שם משפחה: " + lname + "</td></tr> <tr><td> אימייל: " + email + "</td></tr> <tr><td> סיסמא: " +pass +"</td></tr>"); 
    document.write("<tr><td> השחקן האהוב עליך הוא " + favor +"</td></tr>"); 
    document.write("</table>"); 
    document.write("</div></body>"); 
    } 

這裏的表格標題:

<form name ="reg" action ="Javascript:funcs()" onsubmit ="return checkValidation()"> 

我想明確指出,所有的JavaScript代碼可以正常使用,另外,它必須是這個功能的東西。 當我按下發送按鈕時,它不會執行任何操作。任何人都知道最新的問題? 謝謝先進。

+0

是否確定checkValidation返回true? – aquinas 2012-03-22 18:44:30

+2

首先,你的代碼片段中沒有'checkValidation'函數。無法從不存在的函數返回值... – 2012-03-22 18:45:12

+0

Marc B我假設'checkValidation()'是「所有其他正在工作的javascript代碼完美」並且'funcs'是函數不。我可能是錯的。 – 2012-03-22 18:49:44

回答

2

正如我懷疑。一個問題是這一行:

var favor = document.reg.favor[selectedIndex].value; 

應該

var favor = document.reg.favor[document.reg.favor.selectedIndex].value; 

而且你的第二個問題是這樣的:

var pass = document.password.value; 

應該是:

var pass = document.reg.password.value; 

見更新小提琴:http://jsfiddle.net/x7SBy/1/

最後,你應該使用Firefox並下載Firebug。調試這樣的JS問題是非常寶貴的。

編輯:還有其他問題與你的JS,我不會詳細介紹,但一般來說你不想使用document.reg.password,因爲這樣的問題。你應該真的使用document.getElementById。僅供參考。

+0

OHHHHHH非常感謝你!有用!!!!!!!!!!!! – idish 2012-03-22 20:49:55

4

can't 不應該有一個JavaScript函數在action屬性,它需要一個URI。如果驗證成功,您可以致電funcsonsubmit

正如Aquinas所示,在action屬性中調用javascript函數實際上是可行的,因此建議您不要將js代碼放入action屬性中。

+0

你是認真的?我的老師告訴我它可能! – idish 2012-03-22 18:52:18

+0

「此屬性指定表單處理代理。對於HTTP URI以外的值的用戶代理行爲未定義。」我不是說你應該這樣做。事實上,你不應該這樣做,但是在IE 9,Chrome和Firefox中,行爲就是它調用函數。你在哪些瀏覽器中看到不同的行爲? – aquinas 2012-03-22 18:56:15

+0

阿奎那,你可以鏈接到一個小提琴在哪裏工作? – 2012-03-22 19:01:39

0

看起來您正在嘗試驗證表單,如果有效,請調用funcs函數更改頁面上的HTML。

也許是這樣的:

<form name="reg" action="" onsubmit="checkValidation()"> 

然後,checkValidation功能暫停表單提交,如果有效,調用funcs功能:

function checkValidation(e) { 
    e.preventDefault(); 

    if (checkValidation()) { 
     funcs(); 
    } 
} 

但是,如果是這樣的話,你的funcs功能不應該寫<head>標籤等。也許你可以直接將HTML添加到正文中,而不是嘗試使用javascript將新的HTML文檔放置到DOM中。

替代的解決方案

function checkValidation() { 
    ... do your validation 

    return true; // or false if invalid 
} 

然後在form你的動作標籤使用一個真正的HTML頁/資源。