【问题标题】:Dropdown issues clearing selection下拉问题清除选择
【发布时间】:2021-09-03 18:38:57
【问题描述】:

我的下拉菜单运行良好,它显示了所需的信息,但是当我返回顶部选择“你今天想学什么?”之前的信息未清除。 有关如何纠正此问题的任何想法?

var jsonStringProcessing =
   '[{"Question":"Where would I find information on late trips?","Answer":"By using the Late Trip Application you can see incoming mail to your facility.","Link":"https://www.usps.com"},\
     {"Question":"How many DPS errors will I have?","Answer":"Log into the application and drill down by zip 4 facility.","Link":"https://www.usps.com"},\
     {"Question":"What should I do if im missing a trip?","Answer":"Login into the ___ application and drill down by facility.","Link":"https://www.usps.com"},\
     {"Question":"What should I do if a load plan fails?","Answer":"Contact __ via the __ application","Link":"https://www.usps.com"},\
     {"Question":"Where do I report late arriving trips?","Answer":"Click on the below hyper link to submit information.","Link":"https://www.usps.com"},\
     {"Question":"How can I control work hours to plan?","Answer":"By logging into IVES and entering the appropriate information you can plan and schedule employees.","Link":"https://www.usps.com"}]';
var jsObjectProcessing = JSON.parse(jsonStringProcessing);

// fill processing select list
$.each(jsObjectProcessing, function (key, val) {
   $("#processing").append("<option>" + val.Question + "</option>");
});

//$(document).ready(function() {
$("#processing").change(function () {
   var selectedItem = $(this).val();
   $.each(jsObjectProcessing, function () {
      if (this.Question == selectedItem) {
         $("#listProcessing").html(
            '<b>Answer:</b> ' +
               this.Answer +
               '<br>' +
               '<b>Link:</b> <a href="' + this.Link + '" target="_blank">' + this.Link + '</a>'
         );
      }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-6 mb-5 mx-auto">
  <select class="custom-select shadow-sm" id="processing">
    <option value="">What would you like to learn today?</option>
</select>
      <div class="mt-2">
      <div id="listProcessing">
    </div>
  </div> 
</div>

【问题讨论】:

  • 添加一个条件,当所选选项的值 = "" 时清除答案(它不会自动发生 - 您必须添加 JS 代码才能做到这一点)

标签: javascript html dropdown


【解决方案1】:

var jsonStringProcessing =
   '[{"Question":"Where would I find information on late trips?","Answer":"By using the Late Trip Application you can see incoming mail to your facility.","Link":"https://www.usps.com"},\
     {"Question":"How many DPS errors will I have?","Answer":"Log into the application and drill down by zip 4 facility.","Link":"https://www.usps.com"},\
     {"Question":"What should I do if im missing a trip?","Answer":"Login into the ___ application and drill down by facility.","Link":"https://www.usps.com"},\
     {"Question":"What should I do if a load plan fails?","Answer":"Contact __ via the __ application","Link":"https://www.usps.com"},\
     {"Question":"Where do I report late arriving trips?","Answer":"Click on the below hyper link to submit information.","Link":"https://www.usps.com"},\
     {"Question":"How can I control work hours to plan?","Answer":"By logging into IVES and entering the appropriate information you can plan and schedule employees.","Link":"https://www.usps.com"}]';
var jsObjectProcessing = JSON.parse(jsonStringProcessing);

// fill processing select list
$.each(jsObjectProcessing, function (key, val) {
   $("#processing").append("<option>" + val.Question + "</option>");
});

//$(document).ready(function() {
$("#processing").change(function () {
   var selectedItem = $(this).val();
   $.each(jsObjectProcessing, function () {
      if (this.Question == selectedItem) {
         $("#listProcessing").html(
            '<b>Answer:</b> ' +
               this.Answer +
               '<br>' +
               '<b>Link:</b> <a href="' + this.Link + '" target="_blank">' + this.Link + '</a>'
         );
      }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-6 mb-5 mx-auto">
  <select class="custom-select shadow-sm" id="processing">
    <option value="" disabled selected>What would you like to learn today?</option>
</select>
      <div class="mt-2">
      <div id="listProcessing">
    </div>
  </div> 
</div>

你可以这样使用

<option value="" disabled selected>Select your option</option>

【讨论】:

    【解决方案2】:

    由于它不会自动发生,你可以做的是:

    1. 一旦任何选项为selected,就禁用第一个选项。
    2. 如果第一个选项是selected,则添加if 条件。

    var jsonStringProcessing =
       '[{"Question":"Where would I find information on late trips?","Answer":"By using the Late Trip Application you can see incoming mail to your facility.","Link":"https://www.usps.com"},\
         {"Question":"How many DPS errors will I have?","Answer":"Log into the application and drill down by zip 4 facility.","Link":"https://www.usps.com"},\
         {"Question":"What should I do if im missing a trip?","Answer":"Login into the ___ application and drill down by facility.","Link":"https://www.usps.com"},\
         {"Question":"What should I do if a load plan fails?","Answer":"Contact __ via the __ application","Link":"https://www.usps.com"},\
         {"Question":"Where do I report late arriving trips?","Answer":"Click on the below hyper link to submit information.","Link":"https://www.usps.com"},\
         {"Question":"How can I control work hours to plan?","Answer":"By logging into IVES and entering the appropriate information you can plan and schedule employees.","Link":"https://www.usps.com"}]';
    var jsObjectProcessing = JSON.parse(jsonStringProcessing);
    
    // fill processing select list
    $.each(jsObjectProcessing, function (key, val) {
       $("#processing").append("<option>" + val.Question + "</option>");
    });
    
    //$(document).ready(function() {
    $("#processing").change(function () {
       var selectedItem = $(this).val();
       if (selectedItem == ""){
          $("#listProcessing").html("");
       }
       else{
         $.each(jsObjectProcessing, function () {
            if (this.Question == selectedItem) {
               $("#listProcessing").html(
                  '<b>Answer:</b> ' +
                     this.Answer +
                     '<br>' +
                     '<b>Link:</b> <a href="' + this.Link + '" target="_blank">' + this.Link + '</a>'
               );
            }
         });
       }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="col-6 mb-5 mx-auto">
      <select class="custom-select shadow-sm" id="processing">
        <option value="">What would you like to learn today?</option>
    </select>
          <div class="mt-2">
          <div id="listProcessing">
        </div>
      </div> 
    </div>

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 1970-01-01
      • 2011-02-10
      • 2021-09-04
      • 2020-07-07
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      相关资源
      最近更新 更多