我現在正在嘗試使用dijit按鈕驗證框來動態創建表格行。動態創建的驗證框中的Dijit驗證器問題,請幫助
按鈕ID和文本框的id是索引號生成,因此,如何將我的驗證框驗證就可以知道哪個validationbox行是調用驗證?
我應使驗證器:TESTCALL,作爲驗證:功能(){TESTCALL(this.id)},????
function createNewRow() {
var tablebodyname = "RPDetailbody" ;
var mytablebody = document.getElementById(tablebodyname);
var thetabletr = document.createElement('tr');
var thetabletd1 = document.createElement('td');
var thetabletd2 = document.createElement('td');
var thetabletd3 = document.createElement('td');
var thisButton = new dijit.form.Button({
label : thelineindex ,
id : "I_del_butt"+thelineindex,
name : "I_del_butt"+thelineindex,
onClick : function() {
document.getElementById(tablebodyname).removeChild(thetabletr);
}
}).placeAt(thetabletd1) ;
var thisNumberTextBox = new dijit.form.NumberTextBox({
id : "I_seq_no"+thelineindex ,
name : "I_seq_no"+thelineindex ,
value : thelineindex+1 ,
required : "true",
maxLength : 2,
size : 2 ,
style : "width: 2em;",
missingMessage : "Every line must have sequence number"}).placeAt(thetabletd2) ;
var thisValidationTextBox1 = new dijit.form.ValidationTextBox({
id : "I_pig_code"+thelineindex ,
name : "I_pig_code"+thelineindex ,
type : "text" ,
required : "true",
maxLength : 3,
size : 3,
style : "width: 5em;",
validator : testcall ,
missingMessage : "Must have pigment code" }).placeAt(thetabletd3) ;
thetabletr.appendChild(thetabletd1);
thetabletr.appendChild(thetabletd2);
thetabletr.appendChild(thetabletd3);
mytablebody.appendChild(thetabletr);
thelineindex ++ ;
}
<tbody id="RPDetailbody">
<td><div id="delplace"></div></td>
<td><div id="seqplace"></div></td>
<td><div id="pigcodeplace"></div></td>
</tr>
</tbody>
但是我已經儘量做到爲JavaScript函數調用我的驗證,但我的形式submittion發現,使用form.isValid()來驗證所有信息可以通過驗證,它總是返回失敗!
這裏是我的驗證:
function testcall (thebtnID) {
var strlen = thebtnID.length ;
var resultAreaID = "I_pigment_name"+ thebtnID.substring(10, strlen) ;
var pigcode = dijit.byId(thebtnID);
var bNoNameFound = ("Error" == pigcode.get("state")) ? false:true;
var query = "thePig=" + encodeURI(pigcode.value);
if("" == pigcode.value) {
// for some required=true is not kicking in, so we are forcing it.
bNoNameFound = false;
pigcode._setStateClass();
} else {
if(false == pigcode._focused) {
console.log("Checking Pigment...");
dojo.xhrPost({
url: 'ValidatePigmentInput.php',
handleAs: 'text',
postData: query ,
load: function(responseText) {
if (responseText == "no record!") {
// setting the state to error since the pigment is already taken
bNoNameFound = false;
pigcode.set("state", "Error");
//pigcode.displayMessage("The Pigment dosen't exist...");
document.getElementById(resultAreaID).innerHTML = "The Pigment dosen't exist...";
// used to change the style of the control to represent a error
pigcode._setStateClass();
} else {
if (responseText == "pigment BANNED!") {
bNoNameFound = false;
pigcode.set("state", "Error");
document.getElementById(resultAreaID).innerHTML = "Pigment BANNED! Please don't use it!";
pigcode._setStateClass();
} else {
bNoNameFound = true;
pigcode.set("state", "");
document.getElementById(resultAreaID).innerHTML = responseText;
pigcode._setStateClass();
}
}
},
error: function(responseText) {
document.getElementById(resultAreaID).innerHTML = "Error";
}
});
}
}
return bNoNameFound;
}
我想你必須通過包裝函數顯式傳遞id,是的。但是你說'form.isValid()'總是返回false - 你確定這是因爲'testcall',還是可能是因爲窗體中的其他窗口小部件無效? – Frode 2011-06-16 13:08:59
我明白了,isValid總是返回false,因爲我缺少pigcode._setStateClass();在bNoNameFound = true的部分; – Colacat 2011-06-17 02:19:10