How do I access values created by serializeArray in JQuery?

前端 未结 8 636
眼角桃花
眼角桃花 2021-02-02 09:33

I have this HTML:

And I create an object array from it lik

相关标签:
8条回答
  • 2021-02-02 10:22

    Run console.log(dataArray);, then open up the property inspector, and check the console. In Chrome, you'd right click and select "Inspect Element" and then click the ">=" looking icon in the bottom left, it's the second from the left.

    In Firefox you'd install firebug and there's a tab called "Console"

    Not sure if it's available in IE, probably something in the developer tools (press f12) but i wouldn't recommend developing in IE.

    Anyway this will list out the object in a way that allows you to navigate and see the values of each item. That way you can then use this to decipher how to access the values :)

    Good luck

    0 讨论(0)
  • 2021-02-02 10:24

    With uderscore.js, this is how we can go about it:

    var serializedArray = $('form#spiderman-application').serializeArray();
    
    var serializedObject = _.object(
      _.pluck(serializedArray, 'name'), 
      _.pluck(serializedArray, 'value')
    )
    
    console.log(serializedObject);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
    
    <form id="spiderman-application">
      <legend>Application Form</legend>
      <input name="./fname" value="Peter" />
      <input name="./lname" value="Parker" />
    </form>

    Good Luck...

    0 讨论(0)
提交回复
热议问题