2011-03-16 56 views
0

我寫了下面的代碼來計算nXn矩陣的行列式。 我可以成功檢查用戶輸入的訂單(即n)是否有效。 但是我現在被卡住了: 如果我輸入3,我必須顯示9個文本框,每個文本框都必須檢查。首先,我如何顯示一般n^2個文本框(即n行和n列)。 其次我如何檢查他們每個人(即它是一個有效的條目)。顯示特定號碼。 HTML輸入字段使用javascript

代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Javascript App</title> 
<script type="text/javascript" src="app1.js"> 

</script> 
</head> 
<body><h1 id="heading1" style="text-align:center; height:auto; width:auto; font-family:'Arial Black', Gadget, sans-serif">Determinant of a nxn matrix</h1> 
<p id="paragraph1" style="font-family:'Arial Black', Gadget, sans-serif"> This program allows you to compute the determinant of a nxn matrix</p> 

<p> 
Input the order of the matrix 
<br /> 

<input type="text" maxlength="3" name="fname" id="value" /> 
<input type="button" value="submit" onclick="verifyorder()" /> 
</p> 
<p id="error"></p> 
<p id="detspace"> 
<form method="post" action="displaydet()" name="matrix"> 



</p> 



</body> 
</html> 

的javascript:

var order; 
function verifyorder(){ 

    order=document.getElementById('value').value; 
    if(order>0){ 
    displaydet(); 
    } 
    else{ 
     alert("Sorry, you need to enter a positive integer value, try again"); 
    document.getElementById('error').innerHTML="Sorry, you need to enter a positive integer value, try again"; 

    } 

    } 

回答

3

您將需要動態生成的文本框,因爲你不知道有多少會提前。爲此,請使用Document Object Model(DOM),或者使用像JQuery這樣的框架,使DOM更易於使用。

在這種情況下,您需要創建n * n個文本字段,並將它們存儲在兩個不同的「網格」意義上的網格中。首先,你需要將它們存儲在一張物理的HTML表格中,這樣用戶就可以將它們視爲一個網格(回答問題1)。其次,你會想將它們存儲在一個2D JavaScript數組中,所以你的代碼可以操縱它們(回答問題2)。

因此,請使用document.createElement("input")來創建每個文本字段。使用setAttribute方法來設置文本字段的屬性,例如其"value"屬性以將文本存儲在其中(這些屬性與您在靜態HTML源文本字段中看到的屬性相同)。然後,將文本字段存儲在二維數組中,以便稍後可以引用它們。您還應該動態創建HTML表格(使用DOM),動態創建行(tr)和單元格(td),然後在生成的文本字段中插入每個td

一旦你完成了所有這些,用戶可以看到所有的文本字段。當您需要驗證它們時,只需返回您創建的二維數組。在每個文本字段上使用getAttribute("value")以將它們的值作爲字符串返回。然後,您可以根據自己的喜好進行驗證。

-2
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script type="text/javascript" language="javascript"> 
function validation_reg() 
{ 

var f1=document.form1; 
var vname=f1.Login_Name.value; 
var Password=f1.Password.value; 
var submitv=f1.submita.value; 


{ 
    alert(vname); 
    alert(password); 

} 
} 
</script> 
</head> 

<body> 
<form name="form1" id="form1" action="" method="POST"> 
<table align="center" border="1" cellpadding="5"> 
    <tr> 
     <td>Login Name*</td> 
     <td><input id="cname" input name="Login_Name" size="30" type="text" title="Your login name, please!" class="required" maxlength="20" /></td> 
    </tr> 
    <tr> 
     <td> Password*</td> 
     <td><input id="cpaswd" input name="Password" size="31" type="password" maxlength="12" title="Your Password, please!" class="required password" /> 
     <br /></td> 
    </tr> 
    <tr> 
    <td><input class="submit" name="submita" type="submit" value="Submit" onclick="return validation_reg()"/></td></tr> 
    </table> 
    </form> 
</body> 
</html> 
+0

這是怎麼回答的問題?也許關於你想要做什麼或展示什麼的評論會很好。 – 2014-01-17 15:58:51