0

我试图在警报中提醒索引、值和文本。

我有这个代码:

$(document).ready(function() {
  $('#idea_store_ids').chosen().change(function(){
      var doai = $(".search-choice-close").map(function(){
      return $(this).attr("data-option-array-index");}
  ).get();
  $.each(doai , function(index, value) { 
    var storename = $("#idea_store_ids option").map(function() {
             return $(this).text();
          }).get();
    alert(index + ": " + value + ": " + storename); 
  });
  });
});

但是我很难只显示相关的选项名称。它显示所有这些。我认为这是因为没有任何相关的#idea_store_ids 选项和“data-option-array-index”知道哪个属于什么。所以我想知道索引两个选择器并以这种方式关联它们是否是个好主意。

我该怎么做?

HTML:

<input type="hidden" value="" name="idea[store_ids][]"></input>
<select id="idea_store_ids" name="idea[store_ids][]" multiple="multiple" style="display: 
none;">

<option value="103">4 Wheeling Shirts-To-Go</option>
<option value="79">Aliens & Paranormal Shirts-To-Go</option>
<option value="63">America Shirts-To-Go</option>
<option value="2" selected="selected">Ann Arbor T-shirt Company</option>
<option value="19">Area Code Shirts To Go</option>....


<ul class="chosen-choices">
<li class="search-choice">
    <span> … </span>
    <a class="search-choice-close" data-option-array-index="3"></a>
</li>
4

2 回答 2

0

我认为 selected 会重建您的结构,因此您的地图函数不会返回任何信息,您可以使用

console.dir(doai)

例如在 chrome 和 ff 上,您可以看到没有返回任何值,使用元素检查器观察所选内容如何重建标记我认为它甚至有自己的事件侦听器来更改输入

尝试获取选定的值,例如:

    var  storename = $.map( $(this).find("option:selected").val(), function(n){
          return this.value;
   });
于 2013-10-15T17:17:02.243 回答
0

只需在您的内部执行此操作change

alert(this.selectedIndex + ": " + this.value + ": " + $(this).find("option:selected").text()); 

或者:

var index = this.selectedIndex,
    value = this.value,
    text  = $(this).find("option:selected").text();

alert(index + ":" + value + ":" + text);
于 2013-10-15T19:40:37.433 回答