简体   繁体   English

如何添加适用于两个下拉菜单的“随机”按钮

[英]How to add a “random” button that applies to two drop down menus

I have created a page with two drop-down menus containing various values. 我创建了一个带有两个包含各种值的下拉菜单的页面。 Now I would like to add a "randomize" button. 现在,我想添加一个“随机化”按钮。 When clicked, this button would select any of the values at random in both fields. 单击后,此按钮将在两个字段中随机选择任何值。 (the values are also copied on a box above each menu). (这些值也会复制到每个菜单上方的框中)。

Project idea for drop down menus 下拉菜单的项目构想

So far I've coded the menus and the words display in the boxes above them when the user selects them. 到目前为止,我已经对菜单进行了编码,并且当用户选择菜单时,单词将显示在菜单上方的框中。 But now I'm trying to add a randomise button that would put any of the values in the drop down as selected and of course displayed in the above text box. 但是,现在我尝试添加一个随机化按钮,该按钮会将所有值按选定的形式放置在下拉列表中,并且当然会显示在上面的文本框中。 Ideally, more values in the drop-down menus would be added every once in a while without making the script dysfunctional... and ideally it would all be contained in a HTML file (calling for JQuery or javascript is ok). 理想情况下,会不时地在下拉菜单中一次添加更多的值,而不会使脚本运行不正常...并且理想情况下,它们都将包含在HTML文件中(可以调用JQuery或javascript)。

I've looked at this but it doesn't apply. 我看了这个 ,但它并不适用。

I also looked at this but it's not really a feature that the user activates. 我也查看了此内容,但这并不是用户激活的功能。

Very grateful if anyone can help! 非常感谢任何人的帮助! Thanks 谢谢

hope this helps HTML : 希望这对HTML有帮助:

<select id="s1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select id="s2">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Javascript: 使用Javascript:

var minNumber = 0;
var maxNumber = 3;

function randomNumberFromRange(min,max)
{
    return  Math.floor(Math.random()*(max-min+1)+min);


}


$("#s2")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
$("#s1")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);

I create this fiddle for you... https://jsfiddle.net/s59g8vdp/ 我为您创建了这个小提琴... https://jsfiddle.net/s59g8vdp/

This will create random number and click on random div menu: 这将创建随机数,然后单击随机div菜单:

    $(function(){
$(".menu").click(function(){
    alert("you clicked "+$(this).text());
    });
}); 

function doSomthing(){

   var rand=Math.floor((Math.random() * 5));
   alert(rand+1);
   var ele = $(".menu").get(rand);
   $(ele).click();

}

fiddle example: link 小提琴的例子: 链接

Or you can use this example to put value in the menu, or wherever you want: link 2 或者,您可以使用此示例在菜单中或任何需要的地方放置值: 链接2

just a concept how to make that 只是一个概念,如何做到这一点

$('input').on('click',function(){
    var randomnum = Math.floor(Math.random()*$('ul li').length);
    alert($('ul li').eq(randomnum).text());
});

DEMO FIDDLE 演示场

The first example you have provided is very close to what you want to do. 您提供的第一个示例与您要执行的操作非常接近。 You just need to: 您只需要:

  1. Get a random value between 0 and options.length - 1 . 获取介于0options.length - 1之间的random值。
  2. Get option[random] and set its selected property to true . 获取option[random]并将其selected属性设置为true
  3. Copy that option's .innerHTML or .value to the .innerHTML or .value of the label on top of the dropdowns. 将该选项的.innerHTML.value复制到下拉菜单顶部的标签的.innerHTML.value

This is all you probably need: 这就是您可能需要的全部:

 function randomizeSelect(selectId, fieldId) { var options = document.getElementById(selectId).children; var random = Math.floor(options.length * (Math.random() % 1)); var option = options[random]; option.selected = true; document.getElementById(fieldId).value = option.innerHTML; // You can use .value instead in both places } var values = {}; window.onload = function(e) { document.onchange = function(e) { // Event Delegation: http://davidwalsh.name/event-delegate var t = e.target; if(t.tagName == "SELECT") document.getElementById(t.id.replace("Select","Label")).value = t.children[t.selectedIndex].innerHTML; } document.oninput = function(e) { var t = e.target; if(t.tagName == "INPUT") { if(values.hasOwnProperty(t.id)) var options = values[t.id]; else var options = document.getElementById(t.id.replace("Label","Select")).children; var currentValue = t.value; for(i in options) { if(options[i].innerHTML == currentValue) { // Can also use .value options[i].selected = true; break; } } } } document.getElementById("randomize").onclick = function(e) { randomizeSelect("leftSelect", "leftLabel"); randomizeSelect("rightSelect", "rightLabel"); } } 
 <input type="text" id="leftLabel" value="Left 1"> <select id="leftSelect"> <option value="l1" selected>Left 1</option> <option value="l2">Left 2</option> <option value="l3">Left 3</option> </select> <input type="text" id="rightLabel" value="Right 1"> <select id="rightSelect"> <option value="r1" selected>Right 1</option> <option value="r2">Right 2</option> <option value="r3">Right 3</option> </select> <button id="randomize">RANDOMIZE</button> 

Randomize Button added see... ;) i hope this solve your problem! 添加了Randomize Button,请参阅...;)我希望这可以解决您的问题!

https://jsfiddle.net/s59g8vdp/1/ https://jsfiddle.net/s59g8vdp/1/

Html : HTML:

<select id="s1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select id="s2">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<a id="randomize" href="#">randomize</a>

Javascript : Javascript:

var minNumber = 0;
var maxNumber = 3;

$("#randomize").click(function(){
    $("#s2")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
$("#s1")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
});

function randomNumberFromRange(min,max)
{
    return  Math.floor(Math.random()*(max-min+1)+min);


}


$("#s2")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);
$("#s1")[0].selectedIndex = randomNumberFromRange(minNumber, maxNumber);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM