简体   繁体   English

serializeArray对selectbox不起作用

[英]serializeArray not working for selectbox

I am trying to convert form data to json and read the values of the fields that are modified upon clicking respective checkbox. 我正在尝试将表单数据转换为json,并在单击相应的复选框时读取被修改的字段的值。 This is working for text fields but not for select box. 这适用于文本字段,但不适用于选择框。 Can any one please take a look at the fiddle and help me read the select box(Headshot) value in "Person" tab. 任何人都可以看看小提琴,并帮助我阅读“人”选项卡中的选择框(爆头)值。

$('#getDataBtn').click (function() {
     var data = $('#userForm').serializeArray();
     var pfId = 1234;
     var person=[];
    //  $.each(data, function(i, field){
    //     person[field.name] = field.value;            
    //  });
    //  personarray['Person']=person;
    //  console.log(JSON.stringify(personarray));
     var personObj = {};
     $.each(data, function(i, field){
        personObj = {};
        console.log(field.name);
        personObj['table name']= "Person";
        personObj['unique id']= "0";
        personObj['profile id']= pfId;
        personObj[field.name] = field.value;
        person.push(personObj)
    });
    personarray['Person']=person;
    console.log(JSON.stringify(personarray));  

});

https://jsfiddle.net/ycf6Ltad/5/ https://jsfiddle.net/ycf6Ltad/5/

serializeArray() will take only those controls which will have name assigned to them. serializeArray()将仅接受已为其分配name那些控件。 Since the dropdown doesn't have any name assigned, it was not listed. 由于下拉列表未分配任何名称,因此未列出。

To correct, assign a name: 要更正,请分配一个名称:

<select class="form-control" id="sel1" name="sel1" disabled>
  <option>Yes</option>
  <option>No</option>
</select>

See this working fiddle . 看到这个工作的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM