返回选择下拉列表jquery的名称

时间:2016-03-27 19:33:41

标签: jquery select dropdown

我试图循环遍历所有选择的下拉列表并返回每个下拉列表的name属性。有没有办法可以做到这一点?

我的HTML:

<form class="form-horizontal" role="form" id="select_distributor_form">
  <div class="form-group">
    <label  class="col-sm-2 control-label">Supplier Name</label>
    <div class="col-sm-10">
        <select class="form-control" name="supplier1">
          <option selected disabled>Select distributor</option>
            <option value="1">Nike</option>
            <option value="2">Adidas</option>
            <option value="3">Reebok</option>
        </select>
        <select class="form-control" name="supplier2">
          <option selected disabled>Select distributor</option>
            <option value="1">Nike</option>
            <option value="2">Adidas</option>
            <option value="3">Reebok</option>
        </select>
        <select class="form-control" name="supplier3">
          <option selected disabled>Select distributor</option>
            <option value="1">Nike</option>
            <option value="2">Adidas</option>
            <option value="3">Reebok</option>
        </select>
        <select class="form-control" name="supplier4">
          <option selected disabled>Select distributor</option>
            <option value="1">Nike</option>
            <option value="2">Adidas</option>
            <option value="3">Reebok</option>
        </select>
    </div>
  </div>
</form>

到目前为止,我已经尝试过:

$("#select_distributor_form select").each(function(i, obj) {

    var supplier_name = $(this).find('option:selected').attr("name")
    console.log(supplier_name) //returns undefined
});

提前致谢!

1 个答案:

答案 0 :(得分:2)

如果要查找select元素上指定的name属性,则无需查找&#34;选项:selected&#34;就像你的代码一样。

只需从select元素中获取name属性:

csv_filename