PDA

View Full Version : Can l set the default focus?



sosyxg
22 Jul 2007, 1:06 AM
Example:

Ext.onReady(function(){
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';

var Username,Password;
var fs = new Ext.form.Form({
labelAlign: 'right',
labelWidth: 60,
waitMsgTarget: 'box-bd'
});

fs.fieldset(
{legend:'请先登录'},
Username = new Ext.form.TextField({
fieldLabel: '用户名',
name: 'Username',
width:190,
maxLength:12,
maxLengthText:'用户名长度大于12!'

}),

Password = new Ext.form.TextField({
fieldLabel: '密码',
name: 'Password',
width:190,
maxLength:12,
maxLengthText:'密码长度大于12!'

})
);

var Save = fs.addButton({
text: '登录',
handler: function(){
//fs.submit({url:'xml-errors.xml', waitMsg:'Loading...'+Username.getValue()});
if(Username.getValue().length < 1)
{
Ext.MessageBox.alert('提示信息:', '请先输入用户名!',function(){
Username.focus();
});
}
else if(Username.getValue().length > 12)
{
Ext.MessageBox.alert('提示信息:', '用户名长度大于12!',function(){
Username.reset();
Username.focus();
});
}
else if(Password.getValue().length < 1)
{
Ext.MessageBox.alert('提示信息:', '请先输入密码!',function(){
Password.focus();

}); }
else if(Password.getValue().length > 12)
{
Ext.MessageBox.alert('提示信息:', '密码长度大于12!',function(){
Password.reset();
Password.focus();

}); }
else
{

}
}
});


var Reset = fs.addButton({
text: '重设',
handler: function(){
fs.reset();
Username.focus();
}
});


fs.render('form-ct');

});


where i set the default focus for the field 'username' when the page load

dj
22 Jul 2007, 11:08 AM
give your username field an id


new Ext.form.TextField({
fieldLabel: '用户名',
name: 'Username',
width:190,
maxLength:12,
maxLengthText:'用户名长度大于12!',
id:"username"
}),


and after rendering the form call focus() of the HTMLElement.



fs.render('form-ct');
Ext.getDom("username").focus();



... that should do the trick - nothing special to Ext. Just plain JavaScript.

sosyxg
22 Jul 2007, 4:57 PM
ok