【问题标题】:have populated radio buttons, want to use submit button to print the value out已填充单选按钮,想要使用提交按钮打印值
【发布时间】:2017-04-28 22:08:19
【问题描述】:

有没有办法从单选按钮中获取值?说它被填充在单选按钮中,选项是 Abuelos、Boogie Burger、Pad Thai、Coalition Pizza、Wild Eggs。有没有办法可以在点击提交按钮后将这些值拉出来并打印出来?

我也不希望将值重定向到另一个页面。我只是希望它在提交按钮下方打印出来。我也不希望用户在点击提交按钮后能够选择一个值

我正在尝试进行投票,其中从多个数组中获取选项,然后有人可以从单选按钮中选择一个值,然后点击提交按钮并打印出他们的选项。这样用户就可以知道他们投了什么票。

部分 HTML 代码:

    <form action="" id="food-form"></form>

Javascript 代码:

var mexicanFood = ["Caliente Mexican", "Abuelos", "Luciana's"],
    asianFood = ["Omoni Korean", "Super Bowl Pho", "Sichuan Chinese", "Tian Fu Asian Bistro"],
    americanFood = ["Boogie Burger", "City Barbeque", "The North End BBQ", "Wolfies Grill", "Bubs", "Fire on the Monon"];
    pizza = ["Coalition Pizza", "Mackenzie River Pizza, Grill & Pub", "Bazbeaux Pizza", "Mellow Mushroom"]
    thaiFood = ["Pad Thai", "Jasmine Thai", "Thai Orchid"]
    notCategory = ["Jamaican Reggae Grill", "Mudbugs", "Yats", "Kolache Factory", ]
    breakfast = ["Wild Eggs", "Egg and I", "Another Broken Egg Cafe", "Cafe Patachou"]

function createRandomArray(arraySize) {
    var allFoods = mexicanFood.concat(asianFood).concat(americanFood).concat(pizza).concat(thaiFood).concat(notCategory).concat(breakfast),
        randomFoods = [];

    if (arraySize <= allFoods.length) {
        randomFoods = [
            mexicanFood[getRandomArrayIndex(mexicanFood)],
            asianFood[getRandomArrayIndex(asianFood)],
            americanFood[getRandomArrayIndex(americanFood)],
            pizza[getRandomArrayIndex(pizza)],
            thaiFood[getRandomArrayIndex(thaiFood)],
            notCategory[getRandomArrayIndex(notCategory)],
            breakfast[getRandomArrayIndex(breakfast)]
        ]; // at least one from each

        // remove the ones that were initially added from each
        allFoods.splice(allFoods.indexOf(randomFoods[0]), 1);
        allFoods.splice(allFoods.indexOf(randomFoods[1]), 1);
        allFoods.splice(allFoods.indexOf(randomFoods[2]), 1);

        for (var i = 0; i < arraySize - 3; i++) {
            var randomIndex = getRandomArrayIndex(allFoods);

            randomFoods.push(allFoods[randomIndex]);
            allFoods.splice(randomIndex, 1);
        }

        return randomFoods;
    }

    return allFoods; // requesting more items of food than the amount available, so just add them all
}

function getRandomArrayIndex(array) {
    return Math.floor(Math.random() * array.length);
}

var randomFoods = createRandomArray(5);

for (var i = 0; i < randomFoods.length; i++) {
    document.getElementById('food-form').innerHTML += '<input type="radio" name="food" value="' + randomFoods[i] + '"> ' + randomFoods[i] + '<br>';
}

【问题讨论】:

  • 当您使用GET方法提交表单时,查询参数food=xxxxx将附加到您的请求url。只需阅读并显示给用户。

标签: javascript html


【解决方案1】:

你可以使用document.querySelector('input[name=food]:checked').value来获取选中的值。

var mexicanFood = ["Caliente Mexican", "Abuelos", "Luciana's"],
  asianFood = ["Omoni Korean", "Super Bowl Pho", "Sichuan Chinese", "Tian Fu Asian Bistro"],
  americanFood = ["Boogie Burger", "City Barbeque", "The North End BBQ", "Wolfies Grill", "Bubs", "Fire on the Monon"];
pizza = ["Coalition Pizza", "Mackenzie River Pizza, Grill & Pub", "Bazbeaux Pizza", "Mellow Mushroom"]
thaiFood = ["Pad Thai", "Jasmine Thai", "Thai Orchid"]
notCategory = ["Jamaican Reggae Grill", "Mudbugs", "Yats", "Kolache Factory", ]
breakfast = ["Wild Eggs", "Egg and I", "Another Broken Egg Cafe", "Cafe Patachou"]

function createRandomArray(arraySize) {
  var allFoods = mexicanFood.concat(asianFood).concat(americanFood).concat(pizza).concat(thaiFood).concat(notCategory).concat(breakfast),
    randomFoods = [];

  if (arraySize <= allFoods.length) {
    randomFoods = [
      mexicanFood[getRandomArrayIndex(mexicanFood)],
      asianFood[getRandomArrayIndex(asianFood)],
      americanFood[getRandomArrayIndex(americanFood)],
      pizza[getRandomArrayIndex(pizza)],
      thaiFood[getRandomArrayIndex(thaiFood)],
      notCategory[getRandomArrayIndex(notCategory)],
      breakfast[getRandomArrayIndex(breakfast)]
    ]; // at least one from each

    // remove the ones that were initially added from each
    allFoods.splice(allFoods.indexOf(randomFoods[0]), 1);
    allFoods.splice(allFoods.indexOf(randomFoods[1]), 1);
    allFoods.splice(allFoods.indexOf(randomFoods[2]), 1);

    for (var i = 0; i < arraySize - 3; i++) {
      var randomIndex = getRandomArrayIndex(allFoods);
      randomFoods.push(allFoods[randomIndex]);
      allFoods.splice(randomIndex, 1);
    }

    return randomFoods;
  }

  return allFoods; // requesting more items of food than the amount available, so just add them all
}

function getRandomArrayIndex(array) {
  return Math.floor(Math.random() * array.length);
}

var randomFoods = createRandomArray(5);

for (var i = 0; i < randomFoods.length; i++) {
  document.getElementById('food-form').innerHTML += '<input type="radio" name="food" value="' + randomFoods[i] + '"> ' + randomFoods[i] + '<br>';
}

function print() {
  var t = document.querySelector('input[name=food]:checked');

  if (t == null)
    console.log('No value selected');
  else
    console.log(t.value);
}
<form action="" id="food-form">
</form>
<input type="submit" id="btn" value="Submit" onClick="print()">

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 2018-05-12
    • 2021-12-24
    • 2013-05-01
    • 2013-04-10
    • 2020-11-13
    • 2011-09-21
    • 2011-04-20
    • 2015-09-19
    相关资源
    最近更新 更多