如何获取选择元素的Selected值

时间:2016-12-25 07:03:36

标签: javascript jquery

我试图将所选选项的值设为var

doma=$('#domain_picker_select').find('option:selected').text();
OR
var doma=$('#domain_picker_select').val(); 

但是我的输出是未定义的,请帮助我为选择选项选择选项的值。

<select name="domain_picker_select"ng-options="domain.value as domain.label for domain in domains.list" id="domain_picker_select">
<option label="global" value="string:global" selected="selected">global</option>
<option label="TOP/Comm" value="string:6b16be7e6f72710f6">TOP/GBP/Comm</option>
<option label="TOP/Custom" value="string:e15e65256f9bd23">TOP/Custom</option>
</select>

3 个答案:

答案 0 :(得分:0)

尝试一下:

  {

  "Title": "Title of post",

  "Date" : "1/1/1990",

  "Content": "h, i am blog"
  }

-

$("select#domain_picker_select").change(function(){
    var doma = $(this).find(":selected").text();
    console.log(doma); // output value to console
});

编辑

如果你想在加载时获取选择:

<select name="domain_picker_select"ng-options="domain.value as domain.label for domain in domains.list" id="domain_picker_select">
    <option label="global" value="string:global" selected>global</option>
    <option label="TOP/Comm" value="string:6b16be7e6f72710f6">TOP/GBP/Comm</option>
    <option label="TOP/Custom" value="string:e15e65256f9bd23">TOP/Custom</option>
</select>

答案 1 :(得分:0)

要获得价值,请使用val()

$("#domain_picker_select").val();

效果很好! https://codepen.io/anon/pen/ZBNxPo

答案 2 :(得分:0)

你能用这种方式:

$("#domain_picker_select").on('change', function(){
    var doma = $(this).val();
    console.log(doma);
})

希望这个技巧能帮到你。 谢谢:)

$("#domain_picker_select").on('change', function(){
  var doma = $(this).val();
  console.log(doma);
})
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

<select name="domain_picker_select"ng-options="domain.value as domain.label for domain in domains.list" id="domain_picker_select">
<option label="global" value="string:global" selected="selected">global</option>
<option label="TOP/Comm" value="string:6b16be7e6f72710f6">TOP/GBP/Comm</option>
<option label="TOP/Custom" value="string:e15e65256f9bd23">TOP/Custom</option>
</select>