【问题标题】:How do I change paragraph text using JavaScript, depending on which radio button is checked in a form?如何使用 JavaScript 更改段落文本,具体取决于表单中选中的单选按钮?
【发布时间】:2019-02-03 11:54:59
【问题描述】:

我想要实现的是有 1 个问题和 4 个可能的答案可供选择(单选按钮)。当用户在表单后按下按钮时,我想要一个段落来告诉他们更多关于他们所做的选择。

我已经在我的 JavaScript 中定位了按钮、段落和单选按钮。每当我选择第一个单选按钮时,我还设法使段落文本出现。但是,如果同时选择了一个单选按钮并且按下了该按钮,我只希望段落中的文本出现。我希望段落中出现不同的内容,具体取决于选择的单选按钮。

任何提示或解决方案将不胜感激 =)

到目前为止,这是我的代码:

<form action="">
  <h1>A friend invites you to a party. You...</h1>
  <br />

  <input id="red" type="radio" name="color" value="red">...bluntly tell your friend you have other priorities. <br/>
  <input id="blue" type="radio" name="color" value="blue">...tell your friend you are finishing a coding assignment tonight. <br/>
  <input id="yellow" type="radio" name="color" value="yellow">...hug your friend and start discussing the outfit. <br/>
  <input id="green" type="radio" name="color" value="green">...thank your friend for inviting you, and tell her you look forward to it. <br/>

</form>

<button> Click me </button>

<p></p>

<script>
  let btn = document.querySelector('button');
  let para = document.querySelector('p');
  let response = document.querySelector('input');

  response.addEventListener('change', myColor);

  function myColor() {
    let choice = response.value;
    if (choice === 'red') {
      para.textContent = 'You´re so red!';
    } else if (choice === 'blue') {
      para.textContent = 'You´re so blue!';
    } else if (choice === 'yellow') {
      para.textContent = 'You´re so yellow!';
    } else if (choice === 'green') {
      para.textContent = 'You´re so green!';
    }
  }
</script>

【问题讨论】:

  • 我有点困惑。您只希望在按下“单击我”时出现相关文本?然后,您需要将您的函数附加到按钮上的 click 事件 - 而不是像现在这样,附加到无线电输入上的 change 事件。
  • 嗨罗宾,你是对的。我应该更清楚地说明这一点。

标签: javascript html radio-button paragraph


【解决方案1】:

如果我理解正确,您可以使用包装到get the selected radio valueform 并检查它。

像这样:

<form action="">
  <h1>A friend invites you to a party. You...</h1>
  <br />

  <input id="red" type="radio" name="color" value="red">...bluntly tell your friend you have other priorities. <br/>
  <input id="blue" type="radio" name="color" value="blue">...tell your friend you are finishing a coding assignment tonight. <br/>
  <input id="yellow" type="radio" name="color" value="yellow">...hug your friend and start discussing the outfit. <br/>
  <input id="green" type="radio" name="color" value="green">...thank your friend for inviting you, and tell her you look forward to it. <br/>

</form>

<button> Click me </button>

<p></p>

<script>
  const form = document.querySelector('form');
  let btn = document.querySelector('button');
  let para = document.querySelector('p');
  let response = document.querySelector('input');

  //response.addEventListener('change', myColor);
  btn.addEventListener('click', myColor);

  function myColor() {
    let choice = form.color.value;
    if (!choice) {
      return;
    } 
    if (choice === 'red') {
      para.textContent = 'You´re so red!';
    } else if (choice === 'blue') {
      para.textContent = 'You´re so blue!';
    } else if (choice === 'yellow') {
      para.textContent = 'You´re so yellow!';
    } else if (choice === 'green') {
      para.textContent = 'You´re so green!';
    }
  }
</script>

【讨论】:

  • 感谢分享,我从没想过要超越querySelector('input[name=color]:checked 的方式,并且已经有很多年没有使用该表格了。完全从我的脑海中消失了:-)
  • 当然。老歌就是好东西:)如果它回答了你的问题,请accept它,这样它就会对其他人有所帮助。祝你好运:)
  • 会这样做,但不是那个问的人:-)
  • 很高兴 :)
【解决方案2】:

1) 当你定义let response = document.querySelector('input'); 时,你需要意识到document.querySelector 只给你一个匹配的HTML 元素。这解释了为什么只有您的第一个单选按钮可以做任何事情。

2) 如果您希望在单击按钮时发生任何事情,只需向按钮添加一个 onclick 句柄 (button.addEventListener('click', myColor);)。

3) 要在一组单选按钮中找到选定的值,您需要绕道而行 - 试试这样(另见 here): 让选择 = document.querySelector('input[name=color]:checked'); 选择 = 选择 && 选择值; 第一行找到名称为color 的选定单选按钮。注意:如果没有选择收音机,这将是null。如果不是null,第二行将用radios 值替换变量的值。否则,它仍然为空,因此您可以检查} else if (choice === null) { 并设置一些文本,要求用户在按下按钮之前选择一种颜色。

我希望这已经足够清楚并且可以帮助您! 问候, 西蒙

【讨论】:

  • 正如 Mosh Feu 在他的回答中指出的那样,对于第 3 点,您可以更轻松地使用 from 属性中内置的浏览器(即此属性)来使用 let choice = document.forms[0].color.value; 查询所选值。休息保持不变
  • 非常感谢您的建议和澄清 Beppo!我真的很感谢这个论坛上的所有帮助。我一定会在第 3 点尝试您的建议)。很多东西要学。希望当我变得更好时,我能够给这个论坛一些支持。到目前为止,所有问题对我来说似乎都很难 =)
猜你喜欢
  • 2013-12-06
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2018-07-21
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多