Discuss / JavaScript / 完整代码 简洁易懂

完整代码 简洁易懂

Topic source
var person = {};

var form = $('#test-form');
var input = form.find('input');
var i;
for (i in input){
    switch(input[i].name){
        case "name":person.name = input[i].value;
            break;
        case "email":person.email = input[i].value;
            break;
        case "password":person.password = input[i].value;
            break;
        case "gender":if (input[i].value =="m" && input[i].checked == true){ person.gender = "male" }else if(input[i].value =="f" && input[i].checked == true){ person.gender = "female"};
         break;
    }
}

var select = form.find('select');
var ops = select.find('option');

for (i in ops){
     if (ops[i].selected == true){
       person.localtion = ops[i].innerText;
       break;
     }
}


json =JSON.stringify(person);

so

wyp0596

#2 Created at ... [Delete] [Delete and Lock User]
json={};
var input = $('#test-form :input[type!=submit]');
input.map(function(){
  var value = this.value;
  if(this.type === "radio"){
    if(this.checked){
      value =  $(this).parent().get(0).innerText;
      json[this.name] = value;
    }
  }else if(this.name == 'city'){
    var children =   $(this).find('option');
    for(var x in children){
      if(children[x].selected){
        value = children[x].innerText;
      }
    }
    json[this.name] = value;
  }else{
    json[this.name] = value;
  }

});
json=JSON.stringify(json);

  • 1

Reply