简体   繁体   English

JavaScript switch语句是否可以包含document.ready .append函数?

[英]Is it possible for a JavaScript switch statement to include document.ready .append function?

I have two select fields on a my page. 我的页面上有两个选择字段。 Inside of the first one are three different options(categories) of which you can select one. 在第一个选项的内部,可以选择三个不同的选项(类别)。 The second one I want to make dependant on the first one, so if for instance you select "category1" in the first select field, the second one should display different select options available under that one category. 我想使第二个依赖于第一个,因此,例如,如果您在第一个选择字段中选择“ category1”,则第二个应显示在该类别下可用的不同选择选项。

This is the direction I am working on but it doesn't work: 这是我正在努力的方向,但是没有用:

 Choose category:<br>
 <select name="category" id="category">
 <option name="category1" >category1</option>
 <option name="category2" >category2</option>
 <option name="category3" >category3</option>
 </select>
 <br>
 Choose between options:<br>
 <select name="option" id="option" onclick="chooseOne()"></select>
 <br><br>

 function chooseOne(){
     var category=document.getElementbyId("category");
     var selection=category.options[category.selectedIndex].text;

 switch (selection){
     case "Category 1":
         $(document).ready(function() {
         $("#secondSelectField").append("<option>Choose first</option>",
                 "<option>Choose secont</option>",
                 "<option>Choose third</option>",
                 "<option>Choose fourth</option>",
                 "<option>Choose fifth</option>");  
         });
         break;

     case "Category 2":
         $(document).ready(function() {
        $("#secondSelectField").append("<option>Choose sixth</option>",
                 "<option>Choose seventh</option>",
                 "<option>Choose eight</option>",
                 "<option>Choose nineth</option>",
                  "<option>Choose tenth</option>"); 
          });
          break;

      case "Category 3":
          $(document).ready(function() {
          $("#secondSelectField").append("<option>Choose eleventh</option>",
                  "<option>Choose tvelfth</option>",
                  "<option>Choose 13</option>",
                  "<option>Choose 14</option>",
                  "<option>Choose 15</option>",
                  "<option>ETC...</option>");   
          });
          break;

      default:
          $(document).ready(function(){
          $("#secondSelectField").append("Nothing selected");
      }

Can someone please tell me if this is a legitimate way of approaching this problem. 有人可以告诉我这是否是解决此问题的合法方法。 If so, what needs to be fixed in the code in order for it to be working. 如果是这样,则需要在代码中修复哪些内容才能使其正常运行。

If this approach is completely wrong, can someone please advise me on how to do it right? 如果这种方法是完全错误的,有人可以建议我正确的做法吗?

While not optimal, yes this would work. 虽然不是最佳选择,但是可以。

when you call $(document).ready(callback) the given callback will be immediately executed if the document ready event has already been fired. 当您调用$(document).ready(callback) ,如果已经触发了文档就绪事件,则将立即执行给定的回调。

Your code has some problems however: 您的代码有一些问题,但是:

  • the text and value in your first select is category1 and you are comparing it to Category 1 您选择的第一个文本和值是category1 ,您正在将其与Category 1进行比较
  • $("#secondSelectField") but the second select's id is option $("#secondSelectField")但是第二个选择的id是option

A better way would be to bind a function to the event when the document is ready: 更好的方法是在文档准备就绪时将函数绑定到事件:

$(document).ready(function() {

    // listen to the click on the select with ID 'option'
    $('select#option').on('click', function () {
        switch ($(this).val()){
            case "Category 1":
                $("#secondSelectField").append("<option>Choose first</option>",
                        "<option>Choose secont</option>",
                        "<option>Choose third</option>",
                        "<option>Choose fourth</option>",
                        "<option>Choose fifth</option>");
                break;

            case "Category 2":
                $("#secondSelectField").append("<option>Choose sixth</option>",
                        "<option>Choose seventh</option>",
                        "<option>Choose eight</option>",
                        "<option>Choose nineth</option>",
                        "<option>Choose tenth</option>");
                break;

            case "Category 3":
                $("#secondSelectField").append("<option>Choose eleventh</option>",
                        "<option>Choose tvelfth</option>",
                        "<option>Choose 13</option>",
                        "<option>Choose 14</option>",
                        "<option>Choose 15</option>",
                        "<option>ETC...</option>");
                break;

            default:
                $("#secondSelectField").append("Nothing selected");
        }
    });

});

You don't need / want to wrap everything in $(document).ready() 您不需要/不想将所有内容都包装在$(document).ready()

You also don't even need a switch statement. 您甚至不需要switch语句。 You can make this more efficient by using a numbers system to figure out what option numbers to put where. 您可以通过使用数字系统找出要放置在何处的选项编号来提高效率。

HTML 的HTML

<select name="category" id="category">
    <option name="category1" value="1">Option 1</option>
    <option name="category2" value="2">Option 2</option>
    <option name="category3" value="3">Option 3</option>
    <option name="category4" value="4">Option 4</option>
    <option name="category5" value="5">Option 5</option>
</select>

jQuery jQuery的

$(document).on('change', '#category', function(){
    //Multiple for category options
    var optMultiple = 5;
    var html = "";
    var select = $('<select id='secondOption>');

    //get the starting and ending number for the series. If you store your option values in
    //an array you can use this to pull the values from the array in the addOption() function

    var startNum = ( ($(this).val()-1) * optMultiple ) + 1;   
    var endNum = (startNum - 1) + optMultiple;

    html = addOption(startNum, endNum);

    $('#secondOption').replaceWith(select.append(html));
    //replace the current second option select box with the new one
});

var addOption = function(startNum, endNum) {
    var html = '';
    for(var i = startNum; i <= endNum; i++){
        html += '<option value="' + i + "' + '>';
        html += 'Choose ' + i;
        html += '</option>';
    }
    return html;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM