从html输入值调用数组

时间:2015-08-19 21:39:03

标签: javascript html arrays

我试图通过用户在网页上选择的输入调用javascript中的数组..

var selectDay = document.getElementById('weekdayOptions').value;

var Monday = ["hi" , "hello" , "test"];

var Tuesday = ["hi" , "hello" , "test"];

var Wednesday = ["hi" , "hello" , "test"];

var Thursday = ["hi" , "hello" , "test"];

var Friday = ["hi" , "hello" , "test"];

alert(selectDay[2]);

javascript是......

import nltk
from nltk.tag.brill import *
import nltk.tag.brill_trainer as bt
from nltk.corpus import brown

Template._cleartemplates()
templates = fntbl37()
tagged_sentences = brown.tagged_sents(categories = 'news')
tagged_sentences = tagged_sentences[:]
tagger = nltk.tag.BigramTagger(tagged_sentences)
tagger = bt.BrillTaggerTrainer(tagger, templates, trace=3)
tagger = tagger.train(tagged_sentences, max_rules=250)
print(tagger.evaluate(brown.tagged_sents(categories='fiction')[:]))
print(tagger.tag("Hi I am Harry Potter."))

数组的值只是一个例子,在我的代码中它们是不同的,但我想要做的是调用用户输入选择的工作日数组,希望我清楚地解释清楚,任何帮助都很棒,干杯。

3 个答案:

答案 0 :(得分:3)

您正在尝试使用字符串(例如“星期一”)来引用变量名称,这将无效(没有eval,您应该避免)。你最好把一周的日子变成对象键。

var options = document.getElementById('weekdayOptions');
var weekdays = {
  Monday: ["hi" , "hello" , "test"],
  Tuesday: ["hi" , "hello" , "test"],
  Wednesday: ["hi" , "hello" , "test"],
  Thursday: ["hi" , "hello" , "test"],
  Friday: ["hi" , "hello" , "test"]
};

options.onchange = function() {
  var selectDay = this.value;
  alert(selectDay + ': ' + weekdays[selectDay][2]);
}
<select id="weekdayOptions">
  <option>Monday</option>
  <option>Tuesday</option>
  <option>Wednesday</option>
  <option>Thursday</option>
  <option>Friday</option>
</select>

答案 1 :(得分:0)

这是我建议的方法:

<强> HTML

<select id="weekdayOptions">
    <option value="M">Monday</option>
    <option value="T">Tuesday</option>
    <option value="W">Wednesday</option>
    <option value="Th">Thursday</option>
    <option value="F">Friday</option>
</select>

<强>的JavaScript

var days = {
    M: ['monday 1', 'monday 2', 'monday 3'],
    T: ['tuesday 1', 'tuesday 2', 'tuesday 3'],
    W: ['wednesday 1', 'wednesday 2', 'wednesday 3'],
    Th: ['thursday 1', 'thursday 2', 'thursday 3'],
    F: ['friday 1', 'friday 2', 'friday 3']
}

var daySelect = document.getElementById('weekdayOptions');
daySelect.onchange = function() {
    alert(days[this.value][2]);
};

尝试here

答案 2 :(得分:0)

另一种方法如下。 请注意,以便在用户离开星期一时不指定选择框的情况下指定默认值。

http://plnkr.co/edit/d44YNelPByM3ZTibKe8W

<强> HTML

<select id="weekdayOptions">
    <option>Monday</option>
    <option>Tuesday</option>
    <option>Wednesday</option>
    <option>Thursday</option>
    <option>Friday</option>
</select>

<强>的Javascript

var selectDay = document.getElementById('weekdayOptions'); 
var selOptionArray = ["MONhi" , "hello" , "MONtest"]; // Default for Monday: If the selectbox is left unchanged there is no value assigned to the array.

selectDay.onchange = function(){

    switch(selectDay.value){
        case 'Monday':
            selOptionArray = ["MONhi" , "hello" , "MONtest"];
            break;
        case 'Tuesday':
            selOptionArray = ["TUEhi" , "hello" , "TUEtest"];
            break;
        case 'Wednesday':
            selOptionArray = ["WEDhi" , "hello" , "WEDtest"];
            break;
        case 'Thursday':
            selOptionArray = ["THUhi" , "hello" , "THUtest"];
            break;
        case 'Friday':
            selOptionArray = ["FRIhi" , "hello" , "FRItest"];
            break;
        default:
            selOptionArray = "";
            break;
    }

     alert(selOptionArray[2]);
}