从不同的数组

时间:2016-03-25 23:23:43

标签: javascript random categories

我有一个带有下拉菜单的表单,其中包含一些类别(string)。当用户提交表单时,我想显示用户选择的类别中的随机文本,但我不知道该怎么做。

我为一个类别做过,但我不知道如何为后续类别做这件事。

<script language="JavaScript">
<!--
var r_text = new Array ();
r_text[0] = "text1";
r_text[1] = "text2";
r_text[2] = "text3";

var i = Math.floor(3*Math.random())
document.write(r_text[i]);
//-->
</script>

3 个答案:

答案 0 :(得分:1)

这样的事情?

&#13;
&#13;
def dosomething(server):
    stuff = []
    try:
        # function here
        stuff = # function stuff here
    except Exception:
        pass
    return stuff

def getdata(self):
    serverlist = [servera, serverb, serverc]
    data = []
    for server in serverlist:
        results = dosomething(server)
        data.append(results)
    return data
&#13;
function select(){
  var categories = document.getElementById("mySelect").value;
  if(categories == "Funny"){
    var r_text = new Array ();
    r_text[0] = "funny_text1";
    r_text[1] = "funny_text2";
    r_text[2] = "funny_text3";

    var i = Math.floor(3*Math.random())
     document.getElementById("res").innerHTML = r_text[i];
  }else if (categories == "Serious"){
    var r_text = new Array ();
    r_text[0] = "Serious_text1";
    r_text[1] = "Serious_text2";
    r_text[2] = "Serious_text3";

    var i = Math.floor(3*Math.random())
     document.getElementById("res").innerHTML = r_text[i];
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果您想要一种基本的,非数据库方式来执行此操作,您可以执行以下操作:

&#13;
&#13;
  var funny = ["AHAHAH.", "This is funny.", "Ok, don't judge."];

  var other = ["Quote 1", "Quote 2", "Quote 3"];


var category = funny;

var quote = category[[Math.floor(Math.random() * category.length)]];

document.getElementById("output").innerHTML = quote;
&#13;
<div id="output"></div>
&#13;
&#13;
&#13;

变量quote将是您显示的内容,变量category将是用户选择的内容。

答案 2 :(得分:0)

我建议使用包含引号的对象数组以及适用于它们的任何适用标记。因为许多引语可能是有趣,鼓舞人心,爱国等的组合。

您可以使用以下内容:

&#13;
&#13;
var quotes = [
    {
        'value': 'Ask Not What Your Country Can Do For You...',
        'tags': ['inspirational', 'patriotic']
    },
    {
        'value': 'It Ain\'t Over Till It\'s Over',
        'tags': ['inspirational', 'funny']
    },
    {
        'value': '60% Of The Time, It Works Every Time',
        'tags': ['funny']
    },
    {
        'value': 'Funny and serious quote',
        'tags': ['funny', 'serious']
    },
    {
        'value': 'Inspirational and serious quote',
        'tags': ['inspirational', 'serious']
    },
    {
        'value': 'All tags quote 1',
        'tags': ['inspirational', 'serious', 'funny', 'patriotic']
    },
    {
        'value': 'All tags quote 2',
        'tags': ['inspirational', 'serious', 'funny', 'patriotic']
    },
    {
        'value': 'All tags quote 3',
        'tags': ['inspirational', 'serious', 'funny', 'patriotic']
    },
    {
        'value': 'All tags quote 4',
        'tags': ['inspirational', 'serious', 'funny', 'patriotic']
    }
    
        // Etc...
    ];

var getQuoteButton = document.getElementById('get-quote');

function getQuote() {
  
    var currentTag, randomIndex, randomQuote;
    
    var matchingQuotes = [];
    
    var category = document.getElementById('quote-category').value;
  
    if (category !== 'all') { 
  
        // Iterate through list of all quotes
        for (var quoteIndex = 0, quotesLen = quotes.length; quoteIndex < quotesLen; quoteIndex++) {
        
            // Iterate through current quote's tags
            for (var tagIndex = 0; tagIndex < quotes[quoteIndex].tags.length; tagIndex++) {
          
                currentTag = quotes[quoteIndex].tags[tagIndex];
          
                if (currentTag === category) {
                    
                    // Add matching quotes to array
                    matchingQuotes.push(quotes[quoteIndex].value);
                
                }
          
            }
      
        }
      
        randomIndex = Math.floor(Math.random() * matchingQuotes.length);
        randomQuote = matchingQuotes[randomIndex];
      
    }
  
    else {
        
        // If 'All' is selected, choose randomly from all quotes
        randomIndex = Math.floor(Math.random() * quotes.length);
        randomQuote = quotes[randomIndex].value;
      
    }
  
    document.getElementById('quote').innerHTML = randomQuote;
  
}

getQuoteButton.onclick = getQuote;
&#13;
<label>Select A Category
<select name='quote-category' id='quote-category'>
  <option value='all'>All</option>
  <option value='funny'>Funny</option>
  <option value='inspirational'>Inspirational</option>
  <option value='serious'>Serious</option>
  <option value='patriotic'>Patriotic</option>
</select>
<button id='get-quote'>Get Quote</button>
<div id='quote'></div>
&#13;
&#13;
&#13;