Get All of One (or More) Element's Attributes with jQuery

后端 未结 1 2014
攒了一身酷
攒了一身酷 2021-01-28 00:50

I have a requirement to supply values from 2 select boxes to an Action Method. I\'d like to know if there\'s a way to automatically extract all of the attributes from the select

1条回答
  •  后悔当初
    2021-01-28 01:33

    You'd best do this with map():

    var valuesArray = $("select").map(function() {
      return $(this).find(":selected").val();
    });
    

    The above returns an array of values. You may need to identify the source of each value, in which case you'll need something like:

    var values = {};
    $("#select").each(function() {
      values[$(this).attr("name")] = $(this).find(":selected").val();
    });
    

    which creates an anonymous object of all the

    提交评论

提交回复
热议问题