JQM:如何从SELECT(多个)中获取(仅1)所选项目(在选择/单击时)?

时间:2014-03-10 16:26:37

标签: javascript jquery jquery-mobile

编辑:由于我刚接触JQM并且我没有找到一种简单的(JQM内置)方式来保持(保留)订单从MULTIPLE中选择项目时,我打破了算法,我认为我需要你可以满足下面的问题,这样我就可以获得用户在这些项目中的排名选择。 PS我希望尽可能采用jqm方式。

主要问题:如何获取所选项目(仅1)?由于它的倍数,$(this).val()$(this).text()都失败了。

<select name="names[]" multiple="multiple" data-native-menu="false">`
  <option>opt1</option>
  <option>opt2</option>
  <option>opt3</option>
</select>

我有这个jQuery:

$('body').delegate('select','change',function(e) {
  //selected_or_clicked_item = $(this).val();
  //after selecting multiple item - returns an array of all input ['opt1','opt3','opt2']
  selected_or_clicked_item = $(this).text(); // returns all concatenated options text - opt1opt2opt3
});

我只在第一次选择时获得正确的项目(例如,opt1),在第二次选择(opt2)之后我得到了两者(['opt1','opt2']或opt1opt2)

2 个答案:

答案 0 :(得分:0)

试试这个

$('body').delegate('select','change',function(e) {
      //selected_or_clicked_item = $(this).val();
      //after selecting multiple item - returns an array of all input ['opt1','opt3','opt2']
      selected_or_clicked_item = $(this).find('option:selected').text(); // returns all concatenated options text - opt1opt2opt3
    });

答案 1 :(得分:0)

This will work :-

$('body').delegate('select','change',function(e) {
  selected_or_clicked_item_val = $(this).find("option:selected:last").val();     
  selected_or_clicked_item_text = $(this).find('option:selected:last').text(); 
});

I have edited my answer. check this. It will solve your problem.