0
對 http://www.greekforme.com/999-apron-01.html,有幾個下拉框。驗證在下拉框中選擇了一個選項
稱爲「verifyselection」的腳本是爲了顯示一個彈出框,如果用戶不改變降下來,從「選擇一個選項下的」
function verifyselection(form)
{
// result function
var blnResult = true;
// temp name form control
var nameControl = "";
// array of name of radio form controls
var arrNameControl = new Array();
// array of value checked of radio form controls
var arrValueControl = new Array();
// flag existence form control in array
var isExistOnArray = false;
// loop on all elements of form
for(i=0; i<form.elements.length; i++) {
// check type form control
if(form.elements[i].type=="radio") {
// save name form control
nameControl = form.elements[i].name;
// reset flag existence form control in array
isExistOnArray = false;
// loop on all found radio form control
for(j=0; j<arrNameControl.length; j++){
// if giving form control is exist in array
if(arrNameControl[j] == nameControl) {
// set flag
isExistOnArray = true;
// break loop
break;
}
}
// if giving form control is not exist in array
if(isExistOnArray == false){
// set index of array
j = arrNameControl.length;
// add new element to arrays
arrNameControl[j] = nameControl;
arrValueControl[j] = 0;
}
// if giving radio form control is checked
if(form.elements[i].checked == "1"){
arrValueControl[j] = 1;
}
}
if ((form.elements[i].selectedIndex > -1)) {
if (form.elements[i].selectedIndex == 0) {
var opttext = form.elements[i].value.toLowerCase();
if (opttext.indexOf('optional') < 0) {
blnResult = false;
alert('Please select one of the options from the list');
break;
}
}
}
}
// loop on all found radio form control
if(blnResult==true) {
for(j=0; j<arrNameControl.length; j++){
// if radio group form control is checked
if(arrValueControl[j] != 1) {
// set result function
blnResult = false;
// show error message
alert('Please select one of the options from the list');
break;
}
}
}
// return result function
return blnResult;
}
目前,我可以得到彈出當您點擊添加到購物車按鈕時顯示上框 -
但是...它仍然將項目添加到購物車。
我希望腳本,以防止被添加到購物車,如果用戶不改變從「選擇一個選項下的」
啊,我現在看到你的鏈接,我也看到你已經這樣做了。我會繼續尋找:) – 2010-12-20 11:23:02
感謝您重新檢查 - 是的,'回報'包含在onsubmit中。這一次讓我困惑了一段時間! – Michael 2010-12-20 17:45:37
嗨,我現在沒有時間調試它,但是你有機會使用jQuery之類的框架嗎?這會使代碼變成類似'$(form).submit(function(){return $('select option:selected [value!=「」],input [type = radio] [value!=「」] ').length> 0});':) – 2010-12-20 21:47:30