Form.Element.present - Prototype JavaScript 框架

Xunxin Prototype API

present

present(element) -> boolean

如果一个文本输入框包含内容,返回 true,否则返回 false

样例

在常规的表单校验中,这个方法非常有用。尝试提交下面的表单,第一次不要填写任何信息,然后键入一些文本,再次提交:

User Details

Please fill out the following fields:

这是上面的样例所需的完整的 JavaScript 代码:

$('example').onsubmit = function(){ 
	var valid, msg = $('msg')
	
	// 是否所有的字段都已填写? 
	valid = $(this.username).present() && $(this.email).present()
	
	if (valid) { 
		// 在实际应用中,为允许表单提交,我们应返回 true 
		// return true 
		msg.update('Passed validation!').style.color = 'green'
	} 
	else { 
		msg.update('Please fill out all the fields.').style.color = 'red'
	} 
	return false 
}