如何从多选下拉列表中选择最新值

时间:2017-07-03 09:50:41

标签: javascript html

我有一个多选下拉列表,我想获得最新选定值的值。在我的示例中,我只是使用警报来显示所选选项。当我选择' volvo'它提醒沃尔沃,现在如果我按下ctrl并多次选择'欧宝,我仍然会收到警报'沃尔沃'但我希望它提醒“欧宝”。我尝试使用数组来存储值,但我无法在下拉列表中使用第二个选项。

我的实际代码是关于将这些值动态插入表中的新行。但是'沃尔沃'添加evrytime而不是其他选定的选项

以下是代码:



<!DOCTYPE html>
<html>

<body>

  <form action="/action_page.php">
    <select name="cars" onchange="myFucntion(this.value)" multiple>
    <option value="volvo">Volvo</option>
    <option value="bmw">Bmw</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
    </select>
    <input type="submit">
  </form>

  <p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>

  <script type="text/javascript">
    function myFucntion(val) {
      alert(val);
    }
  </script>
</body>

</html>
&#13;
&#13;
&#13;

编辑:只有selectpicker multiselect下拉列表似乎存在问题。当我删除selectpicker类时,它工作正常。在使用selectpicker时,无论如何都要解决它?

5 个答案:

答案 0 :(得分:1)

这将获取选项列表中的最后一个选定项目。

<!DOCTYPE html>
<html>

<body>

  <form action="/action_page.php">
    <select name="cars" onchange="myFucntion(this.value)" multiple>
    <option value="volvo">Volvo</option>
    <option value="bmw">Bmw</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
    </select>
    <input type="submit">
  </form>

  <p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>

  <script type="text/javascript">
    var lastSelected = null;
    function myFucntion(val) {
      alert(lastSelected === null ? 'There was no last selected item' : lastSelected);
      lastSelected = val;
    }
  </script>
</body>

</html>

我怀疑这不是你想要的。您可能想要列表中的所有选定项目:

//Function courtesy of https://stackoverflow.com/a/27781069/4875631
function getSelectValues(select) {
  var result = [];
  var options = select && select.options;
  var opt;

  for (var i=0, iLen=options.length; i<iLen; i++) {
    opt = options[i];

    if (opt.selected) {
      result.push(opt.value || opt.text);
    }
  }
  return result;
}

function myFunction(select) {
    var values = getSelectValues(select);
    console.log(values);
}
<!DOCTYPE html>
<html>

<body>

  <form action="/action_page.php">
    <select name="cars" onchange="myFunction(this)" multiple>
    <option value="volvo">Volvo</option>
    <option value="bmw">Bmw</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
    </select>
    <input type="submit">
  </form>

  <p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>
</body>

</html>

答案 1 :(得分:1)

将事件更改为click并使用target.value应该可以解决问题。

&#13;
&#13;
const elem = document.getElementById('elem');
      elem.addEventListener('click', (e) => console.log(e.target.value));
&#13;
<form action="/action_page.php">
  <select name="cars" id='elem' multiple>
    <option value="volvo">Volvo</option>
    <option value="bmw">Bmw</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
  <input type="submit">
</form>

<p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您需要在选项上放置一个单击处理程序。然后只测试是否选择了该选项(这样当您取消选择该选项时就不会发出警报)。

<!DOCTYPE html>
<html>

<body>

  <form action="/action_page.php">
    <select name="cars"  multiple>
    <option value="volvo" onclick="myFunction(this)">Volvo</option>
    <option value="bmw" onclick="myFunction(this)">Bmw</option>
    <option value="opel" onclick="myFunction(this)">Opel</option>
    <option value="audi" onclick="myFunction(this)">Audi</option>
    </select>
    <input type="submit">
  </form>

  <p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>

<script type="text/javascript">
function myFunction(option) {
    if (option.selected) {
        alert(option.text);
    }
}
</script>
</body>

</html>

答案 3 :(得分:0)

我认为我们必须正确跟踪最近选择的选择值。让我们说用户选择

A - &gt; B-&GT; C-&GT; d

然后最近的选择是D所以我们打印D.现在如果用户取消选择选项D.如果你能够打印C,那么适当的近期跟踪将起作用,因为最近在D之前选择了C。等等C取消选择。上面的所有答案都没有考虑到这一点。所以我在这里有我的答案和逻辑版本。

&#13;
&#13;
$(document).ready(function(){

var selectedValues = [];
$('#carsDdl').on('click',function(event){
  var recentSelection = event.target.value;
  var index = selectedValues.indexOf(recentSelection);
  if(index === -1){
     selectedValues.push(recentSelection);
  }else{
     selectedValues.splice(index,1);
  }
  if(selectedValues.length !== 0){
     var recentSelection = selectedValues[selectedValues.length-1];
     console.log('All selected cars: '+ selectedValues);
     console.log('Recently selected car: '+ recentSelection);
  }
 
})

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <form action="/action_page.php">
    <select id = 'carsDdl' name="cars" multiple>
    <option value="volvo">Volvo</option>
    <option value="bmw">Bmw</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
    </select>
    <input type="submit">
  </form>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

我添加了从多选

中找出追加和删除项目的功能

var old_selected = [];
function getSelectValues(select) {
  var result = [];
  var options = select && select.options;
  var opt;

  for (var i=0, iLen=options.length; i<iLen; i++) {
    opt = options[i];

    if (opt.selected) {
      result.push(opt.value || opt.text);
    }
  }
  return result;
}
function myFucntion(el){
	new_selected = getSelectValues(el);
  //find added elemnts
  added = new_selected.filter(function(item){
  	return old_selected.indexOf(item)==-1
  });
  if(added.length>0)
  	alert(added+' added')
  //find removed 
  removed = old_selected.filter(function(item){
  	return new_selected.indexOf(item)==-1
  });
  if(removed.length>0)
  	alert(removed+' removed')
 
  old_selected = new_selected;
  
}
  

<form action="/action_page.php">
    <select name="cars" onchange="myFucntion(this)" multiple>
    <option value="volvo">Volvo</option>
    <option value="bmw">Bmw</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
    </select>
    <input type="submit">
  </form>

  <p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>