Ext中注册新用户

function addStu(){

  //初始化QuickTips实例。
     Ext.QuickTips.init();
    
var stuname=new Ext.form.TextField({
          msgTarget:'qtip',
              fieldLabel:'用户名',
              allowBlank:false,
              maxLength:18,
              name:'stu.stuname',
              width:160,
              blankText:'用户名不能为空!'
              });
             
  var stuage=new Ext.form.TextField({
          msgTarget:'qtip',
              fieldLabel:'年 龄',
              allowBlank:false,
              maxLength:3,
              name:'stu.stuage',
              minLength:1,
              width:160,
              emptyText:0,
              blankText:'年龄不能为空!'
              }); 
             
var address=new Ext.form.TextField({
          msgTarget:'qtip',
              fieldLabel:'家庭住址',
              allowBlank:false,
              maxLength:18,
              name:'stu.address',
              width:160,
              blankText:'家庭住址不能为空!'
              });

//定义班级的store
var gradeStore=new Ext.data.Store({
proxy:new Ext.data.HttpProxy({
  url:'grade.do?method=showGradeName'
  }),
  reader:new Ext.data.JsonReader({
   root:'gradenamejs'
  },[
  {name:'gradeid'},
  {name:'gradename'}
  ])
});
//定义老师的store
var teacherStore=new Ext.data.Store({
  proxy:new Ext.data.HttpProxy({
   url:'grade.do?method=showTeacher&gradeid=0'//这个地址可以随便给
  }),
  reader:new Ext.data.JsonReader({
   root:'teacherjs'
  },
  [{name:'gradeid'},
  {name:'teacher'}
  ])
});

var gradeField=new Ext.form.ComboBox({
fieldLabel:'班级名称',
allowBlank:false,
mode:'local',
//hiddenName:'stu.grade.gradename',//提交到后台中select中的值
hiddenName:'stu.grade.gradeid',
editable:false,
blankText:'请选择班级名称!',
triggerAction:'all',
emptyText:'请选择班级',
store:gradeStore,
listeners:{
   select:function(combox,record,index){
    teacherFeild.clearValue();//实现下拉框的值的变更
    teacherStore.proxy=new Ext.data.HttpProxy({
       url:'grade.do?method=showTeacher&gradeid='+combox.value 
    });//动态显示老师姓名
    teacherStore.load();
   }
},
valueField:'gradeid',
displayField:'gradename'
});

//加载班级的下拉框的值
gradeStore.load();

//老师的下拉框
var teacherFeild=new Ext.form.ComboBox({
  xtype:'combox',
  store:teacherStore,
  valueField:'gradeid',
  displayField:'teacher',
  forceSelection:true,//强制选择
  editable:false,
  model:'local',
  emptyText:'请选择老师',
// hiddenName:'stu.grade.teacher',//提交到表单的value值
   hiddenName:'stu.grade.gradeid',
  allowBlank:false,
  blankText:'请选择班主任!',
  triggerAction:'all',
  fieldLabel:'班主任'
});       
             
var form=new Ext.form.FormPanel({
              title:'注册',
              labelAlign:'right',
              frame:true,
              autoHeight:'auto',
              items:[
              stuname,
              stuage,
              address,
              gradeField,
              teacherFeild
              ],
              buttons:[{text:'添 加',
              handler:function(){
              //判断输入的信息是否合法
              if(form.getForm().isValid()){
              form.getForm().submit({
             url:'student.do?method=addStudent',method:"post",
                 success:function(form,action){
                  Ext.Msg.alert('信息',action.result.msg);
                  win.close();//关闭窗体
                 },
               failure:function(){
                 Ext.Msg.alert('信息','添加学员信息失败!');
                }
               });
              /* //第二种提交方式
              form.form.doAction('submit',{
url:'student.do?method=addStudent',
method:'post',
params:'',
success:function(form,action)
{
Ext.Msg.alert('信息',action.result.msg);
win.close();//关闭窗体
},
failure:function()
{
  Ext.Msg.alert('信息','添加学员信息失败!');
}
});*/
              }
              }},
              {text:'取 消',handler:function(){
                win.close();//取消关闭窗体
              }}],
              buttonAlign:'center'
              });
      //用组件渲染至HTML元素中       
     //form.render("show");
          stuname.markInvalid();//标记该字段无效  
          stuage.markInvalid();
          address.markInvalid();
             
var win=new Ext.Window({
              title:'添加学员信息',
              autoHeight:true,
              width:400,
              resizable:false,
              items:[form]
              });             
     win.show();  
}

你可能感兴趣的:(ext)