【问题标题】:How do I display different input boxes based on choices from an option box?如何根据选项框的选择显示不同的输入框?
【发布时间】:2017-12-18 14:39:30
【问题描述】:

我正在尝试创建一个表单,让您可以输入您的 SAT 或 ACT 分数。一旦您选择了您参加的考试,它应该会根据您选择的考试是 SAT 还是 ACT 显示不同的后续问题。这是我到目前为止所拥有的。它几乎可以工作,但不完全。这是我的代码。下拉框工作正常,但我无法显示后续问题。任何帮助将不胜感激。

这是数据库的简化版本:

  • “test_id”“test_name”“test_type”
  • 1“2017 年 6 月 ACT”“ACT”
  • 2“2017 年 4 月 ACT”“ACT”
  • 3“2017 年 6 月 SAT”“SAT”
  • 4“2017 年 5 月 SAT”“SAT”

下拉框显示 test_name,并将 test_id 作为 value,将 test_type 作为 rel。

function test_scores() {
  var test_type = document.getElementById('test_name').rel;
  if (test_type = "ACT") {
    act_scores.style.display = "block";
  }
  if (test_type = "SAT") {
    sat_scores.style.display = "block";
  }
}
.act_scores {
  display: none;
  /* Hidden by default */
}

.sat_scores {
  display: none;
  /* Hidden by default */
}
<form id="msform" onSubmit="return validate();" action="" class="form" method="post" accept-charset="utf-8">
  <h2 class="page-title legend">Score Information</h2>
  <hr>
  <div class="row">
    <div class="col-sm-6">
      <div class="form-group" style="margin-top:20px;"><label>Add Scores:</label>
        <div class="select span5 no-float noMarginLeft">
          <select name="test_name" id="test_name" onchange=test_scores() required="required" style="width:100%;background:#fff;">
        <option value="">Test:</option>
        <option value="1" rel="SAT">Jun 2017 SAT</option>
        <option value="2" rel="ACT">Jun 2017 ACT</option>
        <option value="3" rel="SAT">May 2017 SAT</option>
        <option value="4" rel="ACT">Apr 2017 ACT</option>
          /* php code that is working */
        </select>
        </div>
      </div>
    </div>
    <div id="my_act_scores" class="act_scores">
      <div class="scores-content">
        <span class="close">×</span>
        <label>English:<span style="color: red;width: 20px;">*</span></label>
        <textarea name="act_english" rows="1" class="form-control" required="required" id="act_english"></textarea>
      </div>
      <div class="scores-content">
        <label>Math:<span style="color: red;width: 20px;">*</span></label>
        <textarea name="act_math" rows="1" class="form-control" required="required" id="act_math"></textarea>
      </div>
      <div class="scores-content">
        <label>Reading:<span style="color: red;width: 20px;">*</span></label>
        <textarea name="act_reading" rows="1" class="form-control" required="required" id="act_reading"></textarea>
      </div>
      <div class="scores-content">
        <label>Science:<span style="color: red;width: 20px;">*</span></label>
        <textarea name="act_science" rows="1" class="form-control" required="required" id="act_science"></textarea>
      </div>
      <div class="scores-content">
        <label>&nbsp;</label>
        <button type="submit" name="act_submit" class="btn btn-green black">Submit</button>
        <span id="info"></span></div>
    </div>
    <div id="my_sat_scores" class="sat_scores">
      <div class="scores-content">
        <span class="close">×</span><label>Reading (out of 40):<span style="color: red;width: 20px;">*</span></label>
        <textarea name="sat_reading" rows="1" class="form-control" required="required" id="sat_reading"></textarea>
      </div>
      <div class=" scores-content ">
        <label>Writing (out of 40):<span style="color: red;width: 20px; ">*</span></label>
        <textarea name="sat_writing " rows="1 " class="form-control " required="required " id="sat_writing "></textarea>
      </div>
      <div class="scores-content">
        <label>Math (out of 40):<span style="color: red;width: 20px; ">*</span></label>
        <textarea name="sat_math" rows="1" class="form-control" required="required" id="sat_math"></textarea>
      </div>
      <div class="scores-content ">
        <label>&nbsp;</label>
        <button type="sat_submit" name="submit" class="btn btn-green black ">Submit</button><span id="info"></span>
      </div>
    </div>
  </div>
</form>

【问题讨论】:

  • 如果没有 PHP 问题,请删除所有 PHP 并添加与问题相关的选项和其他 HTML。 sn-p 编辑器中的框不带脚本或样式标签
  • 另外,您还缺少结束引号等等 - 您需要修复一些 HTML 错误
  • 好的,我摆脱了 php 代码。抱歉,我是新人。
  • 你能告诉我我的 html 错误在哪里吗? html大部分都在工作。页面加载。下拉框功能正常。我只是无法显示正确的后续问题。
  • 我修复了 HTML - henrique 修复了你的脚本

标签: javascript select drop-down-menu onchange display


【解决方案1】:

你必须抓取 dom 中的元素来设置样式:

function test_scores(event) {
  var testTypeSelect = event.target;
  var test_type = testTypeSelect.options[testTypeSelect .selectedIndex].getAttribute('rel')
  if (test_type == "ACT") {
    document.getElementById('my_act_scores').style.display = "block";
    document.getElementById('my_sat_scores').style.display = "none";
  }
  if (test_type == "SAT") {
    document.getElementById('my_sat_scores').style.display = "block";
    document.getElementById('my_act_scores').style.display = "none";
  }
}
--//and
<select name="test_name" id="test_name" onchange=test_scores(event)

【讨论】:

  • 谢谢。我做了这些改变。它仍然不能完全按照我想要的方式工作。我不确定我的 var test_type 计算是否正确。我会努力解决这个问题。
  • 尝试 document.getElementById('test_name').value。并放置控制台或调试器进行测试。
  • div 显示上面的代码 - 当我更改 sn-p 以使用它时。 “不完全按照我想要的方式工作”是什么意思?
  • 无论我选择哪一个,ACT 和 SAT 的后续问题都会打开
  • 所以,我的数据库表基本上是这样的: test_id test_name test_type 1 Jun 2017 ACT ACT 2 Apr 2017 ACT 3 Jun 2017 SAT SAT 4 May 2017 SAT SAT 所以,下拉框显示 test_name 和捕获值 test_id,但也将 rel 设置为 test_type。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 2015-02-23
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多