⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bs_formutil.lib.js

📁 征服ajax+lucene书籍各章节源代码ajax部分
💻 JS
📖 第 1 页 / 共 2 页
字号:
/*** returns the form element for a given field element.* @param  element fieldElm* @return element (form element)* @throws bool false* @since  bs-4.6*/function bsFormGetFormForField(fieldElm) {	if (document.forms.length == 1) return document.forms[0]; //optimization	if (fieldElm.tagName == 'form') return fieldElm;	if (fieldElm.parentNode) return bsFormGetFormForField(fieldElm.parentNode);	return false;}/*** returns the next form field, if any.* @param  element fieldElm* @return element (field element)* @throws bool false (error or none)* @since  bs-4.6*/function bsFormGetNextField(fieldElm) {	var formElm = bsFormGetFormForField(fieldElm);	if (!formElm) return false;	var useNext = false;	for (var i=0; i<formElm.elements.length; i++) {		if (useNext) return formElm.elements[i];		if (formElm.elements[i] == fieldElm) useNext = true;	}	return false;}/*** form utility functions.* * @author     andrej arn <andrej-at-blueshoes-dot-org>* @package    javascript_core* @subpackage form*/function bsFormToggleCheckbox(formName, fieldName) {	try {	  if (document.forms[formName].elements[fieldName].checked) {  	  document.forms[formName].elements[fieldName].checked = false;	  } else {  	  document.forms[formName].elements[fieldName].checked = true;	  }	} catch (e) {		//never mind, maybe field not there in this case.	}}function bsFormToggleContainer(containerName) {	if (document.getElementById) {		var elm = document.getElementById(containerName);		if (typeof(elm) != 'undefined') {			elm.style.display = (elm.style.display == 'none') ? 'block' : 'none';						var img = document.getElementById('contToggleImg' + containerName.substr(9));			if (typeof(img) != 'undefined') {				if (elm.style.display == 'none') {					img.src = img.src.substr(0, img.src.length -6) + 'down.gif';				} else {					img.src = img.src.substr(0, img.src.length -8) + 'up.gif';				}			}						var txtO = document.getElementById('contToggleTextO' + containerName.substr(9));			var txtC = document.getElementById('contToggleTextC' + containerName.substr(9));			if (typeof(txtO) != 'undefined') {				if (elm.style.display == 'none') {					txtO.style.display = 'inline';					txtC.style.display = 'none';				} else {					txtO.style.display = 'none';					txtC.style.display = 'inline';				}			}						return;		}	}	if (document.all) {	  if (document.all[containerName].style.display == "none") {	    document.all[containerName].style.display = "block";	  } else {	    document.all[containerName].style.display = "none";	  }	}}/*** @param  string formName* @param  string fieldName* @param  object fieldElement (eg text field)* @return void*/function bsFormCheckOnlyIf(formName, fieldName, fieldElement) {	//alert(fieldName);		if (typeof(bsFormVars) == 'undefined') return;	if (typeof(bsFormVars[formName]) == 'undefined') return;		for (var otherFieldName in bsFormVars[formName]) {		//alert(otherFieldName);		if (typeof(bsFormVars[formName][otherFieldName]['onlyIf']) == 'undefined') continue;		var status = _bsForm_anyIfCase(bsFormVars[formName][otherFieldName]['onlyIf']);		//alert(status);				var elm = document.getElementById(otherFieldName);		if ((typeof(elm) != 'undefined') && (elm != null)) {	    if (!status) { //in this case we need the negation. mustIf is different to onlyIf.				//alert('error onlyIf: ' + otherFieldName);				elm.disabled = true;	    } else {				//alert('ok onlyIf: ' + otherFieldName);				elm.disabled = false;			}		}	}	//if (typeof(bsFormVars['formName']['fieldName']) == 'undefined') return;	//if (typeof(bsFormVars['formName']['fieldName']['onlyIf']) == 'undefined') return;	/*	bsForm['bsUserForm']['toAddress']['onlyIf'] = new Array();	bsForm['bsUserForm']['toAddress']['onlyIf'][0] = new Array();	bsForm['bsUserForm']['toAddress']['onlyIf'][0]['operator'] = "|";	bsForm['bsUserForm']['toAddress']['onlyIf'][0]['field'] = "toAddressType";	bsForm['bsUserForm']['toAddress']['onlyIf'][0]['compare'] = "=";	bsForm['bsUserForm']['toAddress']['onlyIf'][0]['value'] = "hard";	*/	//var a = bsFormVars['formName']['fieldName']['onlyIf'];	/*	for (var i=0; i<a.length; i++) {		if (typeof(a[i]['operator']) == 'undefined') a[i]['operator'] = '|';		if (typeof(a[i]['compare'])  == 'undefined') a[i]['compare']  = '=';		if (typeof(a[i]['value'])    == 'undefined') a[i]['value']    = true;	}	*/}/*** returns the value of the form field specified.* works for select and radio fields too. used with the Bs_Form package.* @param  string elementID* @return mixed*/function bsFormGetFieldValue(elementID) {	var elm  = document.getElementById(elementID);	//alert(elementID);	//alert(elm);	if ((typeof(elm) == 'undefined') || (elm == null) || (elm.id != elementID)) {		var elm  = document.getElementById(elementID + '0');		var elm2 = document[elm.form.name][elm.name];		elm = elm2;	} else {		var elm2 = document[elm.form.name][elm.name];	}		//alert('x ' + elementID);	//alert(elm2[0].value);	//alert(elm2.selectedIndex);	/*	if (typeof(elm2.options) != 'undefined') {		if (elm2.selectedIndex != null) {			return elm2.options[elm2.selectedIndex].value;		}		return null;	}	*/		//radio		for (var i=0; i<elm2.length; i++) {			if (elm2[i].checked) return elm2[i].value;		}	return elm.value;}/*** used for mustIf, onlyIf and mustOneOfIf case. that's where the (method) name anyIf comes from.* @access private* @param  array (object var mustIf, onlyIf or mustOneOfIf, so look there.)* @return bool TRUE if it evaluates to TRUE, FALSE otherwise.*/function _bsForm_anyIfCase(myIf) {  if ((typeof(myIf) == 'object') && (myIf.length > 0)) {		var stack = new Array();		for (i=0; i<myIf.length; i++) {			stack[i] = new Array();      stack[i]['operator'] = (typeof(myIf[i]['operator']) != 'undefined') ? myIf[i]['operator'] : '|';      stack[i]['compare']  = (typeof(myIf[i]['compare'])  != 'undefined') ? myIf[i]['compare']  : '=';      stack[i]['boolean']  = false; //default, fallback.      do { // try block        if (typeof(myIf[i]['value']) != 'undefined') {          var t = bsFormGetFieldValue(myIf[i]['field']);					//alert(t);					//alert(myIf[i]['value']);          if (typeof(t) != null) {            switch (stack[i]['compare']) {              case '=':                stack[i]['boolean'] = (t == myIf[i]['value']);                break;              case '>':                stack[i]['boolean'] = (t > myIf[i]['value']);                break;              case '<':                stack[i]['boolean'] = (t < myIf[i]['value']);                break;              case '>=':                stack[i]['boolean'] = (t >= myIf[i]['value']);                break;              case '<=':                stack[i]['boolean'] = (t <= myIf[i]['value']);                break;              case '!=':              case '<>': //not documented but one might type it.                stack[i]['boolean'] = (t != myIf[i]['value']);                break;								/*								//not supported here in js              case 's': //soundex equal                stack[i]['boolean'] = (soundex(t) == soundex(myIf[i]['value']));                break;              case '!s': //not soundex equal                stack[i]['boolean'] = (soundex(t) != soundex(myIf[i]['value']));                break;								*/              default:                //murphy! don't do anything, keep the default which is FALSE.            }            break;          }        } else {          //stack[i]['boolean'] = $this->_form->isFieldFilledIn($myIf[$k]['field']); //converting to bool cause it could be NULL.          break;        }      } while (false);    }        if ((typeof(stack) == 'object') && (stack.length > 0)) {      var evalStr = '';			for (var i=0; i<stack.length; i++) {        if (i > 0) evalStr += (stack[i]['operator'] == '&') ? '&& ' : '|| ';        evalStr += (stack[i]['boolean']) ? 'true ' : 'false ';      }      //evalStr = 'return (' + evalStr + ');';      evalStr = ' (' + evalStr + ');';      //alert(evalStr); //4debug      //our evalStr string now looks like "return (true && true || false || true);"      if (eval(evalStr)) {        return true;      }    }  }  return false;}/*** on-the-fly client/server check for email addresses* @param string url (the url to submit to)* @param object fieldObj (html input field)* @param int checkType (what to check, 1 2 or 3, see php code)* @see Bs_FormFieldEmail.class.php*/function bsFormCheckMail(url, fieldObj, checkType) {  var fieldName = fieldObj.name;  var fieldID   = fieldObj.id;  var email     = fieldObj.value;  var iFrameObj = document.getElementById('bsMailCheck' + fieldName);  url += "?email=" + email + "&checkType=" + checkType;  var zeit = new Date();  url += "&random=" + zeit.getMilliseconds(); //no url-cache please.  //alert(url); //4debug  iFrameObj.src = url;}/*** sets the focus into the first field that was not filled in correctly.* if param doSelect is true: also selects all the content of that field. * @param  string fieldName* @param  string formName* @param  bool   $doSelect (see above)* @return void*/function bsFormJumpToFirstError(fieldName, formName, doSelect) {	try {	  if (document.forms[formName].elements[fieldName]) {	    if (doSelect && (document.forms[formName].elements[fieldName].value != '')) {	      if (document.forms[formName].elements[fieldName].select) {	        //do all fields support the select() method? dunno. let's check it.	        document.forms[formName].elements[fieldName].select();	      }	    }	    if (document.forms[formName].elements[fieldName].focus) {	      //eg select fields don't support the focus() method.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -