2012-01-13 6 views
0

私は3つのドロップダウンを持っています。最初に応じて、2番目と3番目に異なるオプションを表示する必要があります。 オプションは知っているので、AJAXを使用しないようにすることができます。利用可能なすべてのオプションをドロップダウンに入れ、オプションをターゲットにするためのクラス名を付けました。以下の例:動的ドロップダウンを作成する方法Ajaxを使用しないjQuery

まずドロップダウン:

<select name="language" id="language"> 
<option value="0" disabled="disabled" selected="selected">Select ...</option> 
<option value="DA">Danish - DA</option> 
<option value="EN">English - EN</option> 
</select> 

第二には、ドロップダウン:(.queryallは、選択を変更する場合、すべてのオプションを削除するために使用され、.querydaはデンマーク、.queryenのためのすべてのオプションを表示するために使用されます英語のためのすべてのオプションを表示するために使用される)

<select name="status" id="status"> 
<option value="0" disabled="disabled" selected="selected">Select ...</option> 
<option class="queryall queryda">Answered</option> 
<option class="queryall queryda queryen">Closed</option> 
<option class="queryall queryen">Escalated</option> 
</select> 

のjQuery:

<script> 
$("#language").change(function() { 
$("#status .queryall").hide(); 
$("#status .queryac").show(); 
alert("New Status options are available."); 
}); 
</script> 

ここでの質問は、どのように言語をデンマーク語に変更するときに "queryda"を渡すか?

ありがとうございます。

答えて

1
  • 「test.com」
  • だけの組織のために、このフォルダの下にさらに2つを作成して言う、あなたつもりテストそのフォルダを作成します:「CSS」と「JS」
  • は「jqueryのと呼ばれるフォルダを作成します。 「内部 "のjsのmain.css "

  • CSS "というフォルダファイル" "あなたに作成

  • を" index.htmlを "というフォルダのファイル" test.com"

  • あなたに作成します"あなたの "js"フォルダに "main.js"と "data.js"という2つのファイルを作成します。

  • jQuery(1.7.1)のバージョンをダウンロードしてWebサイトwww.jquery.comからダウンロードし、 "test"に入れます。 「jquery-1.7.1.min.js」の名前をつけたり、「index.html」のリンクを修正してください。

今すぐコードに。

これはあなたの "index.htmlを" コードです:これはあなたの "あるmain.css" のコードである

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="css/main.css" /> 
    <script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="js/main.js"></script> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Dynamic Selects</title> 
</head> 
<body> 
    <div id="data"></div><!-- we will load the data JSON string here //--> 
    <div id="wrapper"> 
     <div id="boxes"> 
      <select name="language" id="language" class="select"> 
       <option value="0" disabled="disabled" selected="selected">Select Language</option> 
      </select> 

      <select name="status" id="status" class="select"> 
       <option value="0" disabled="disabled" selected="selected">Select Status</option> 
      </select> 

      <select name="file" id="file" class="select"> 
       <option value="0" disabled="disabled" selected="selected">Select Files</option> 
      </select> 
     </div> 
    </div> 
</body> 

body, html { 
padding: 0; 
margin: 0; 
width: 100%; 
height: 100%; 
background-color: #ddd; 
} 

#wrapper { 
width: 100%; 
height: 100%; 
} 

#boxes { 
width: 500px; 
margin: 0 auto; 
padding: 100px 0px; 
} 

.select { 
width: 160px; 
height: 20px; 
background-color: #ddd; 
} 

#data { 
display: none; 
} 

.highlighted { 
border: 1px solid red; 
} 

これはあなたの "data.js" でありますコード:

var dataObject = { 
"EN": { 
    "title": "English EN", 
    "status": 
     [{ 
      "title": "Answered" 
      ,"questions": 
       [ 
        {"q1":"Question 1"} 
        ,{"q2":"Question 2"} 
        ,{"q3":"Question 3"} 
        ,{"q4":"Question 4"} 
       ] 
     }, 
     { 
      "title": "Closed" 
      ,"questions": 
       [ 
        {"q5":"Question 5"} 
        ,{"q6":"Question 6"} 
        ,{"q7":"Question 7"} 
        ,{"q8":"Question 8"} 
       ] 
     }, 
     { 
      "title": "Escalated" 
      ,"questions": 
       [ 
        {"q9":"Question 9"} 
        ,{"q10":"Question 10"} 
        ,{"q11":"Question 11"} 
        ,{"q12":"Question 12"} 
       ] 
     }] 
}, 

"DA": { 
    "title": "Danish DA", 
    "status": 
     [{ 
      "title": "Answered DA" 
      ,"questions": 
       [ 
        {"qda1":"Question DA 1"} 
        ,{"qda2":"Question DA 2"} 
        ,{"qda3":"Question DA 3"} 
        ,{"qda4":"Question DA 4"} 
       ] 
     }, 
     { 
      "title": "Closed DA" 
      ,"questions": 
       [ 
        {"qda5":"Question DA 5"} 
        ,{"qda6":"Question DA 6"} 
        ,{"qda7":"Question DA 7"} 
        ,{"qda8":"Question DA 8"} 
       ] 
     }, 
     { 
      "title": "Escalated DA" 
      ,"questions": 
       [ 
        {"qda9":"Question DA 9"} 
        ,{"qda10":"Question DA 10"} 
        ,{"qda11":"Question DA 11"} 
        ,{"qda12":"Question DA 12"} 
       ] 
     }] 
} 
}; 

最後に、thちょうどあなたのブラウザで「index.htmlを」ファイルにナビゲートしたり、ローカル/リモートのWebサーバー上でそれを使用して結果を参照してください、今すぐ

var url = 'js/data.js'; // URL to data file. could be full or relative 

// Select box names (ids) 
var langSelect = 'language'; 
var statusSelect = 'status'; 
var questionSelect = 'file'; 

// Load the data object externally 
$.getScript(url,function(){}); 

// on document load, do whatever you need 
$(document).ready(function() { 

}); 

$(document).ajaxComplete(function(r){ 
    // fire when any Ajax requests complete (we get our data from server) 
dMgr.populateLang(); 
}) 

// data manager object. using: dMgr.functionName(); <-- will populate the linked select  box with chosen data. 
var dMgr = { 
// function to populate language select with data 
populateLang: function() { 
    // if dataObject from server is empty, return nothing 
    if (!typeof(dataObject) == 'object') return false; 

    // create select options string with its current content <only first one> (the "select language.." option) 
    var langs = '<option value="">'+$('#'+langSelect+' option:first').html()+'</option>'; 

    // loop dataObject and get it's keys as language values and title as option title 
    for(i in dataObject) { 
     // combine it to existing string 
     langs += '<option value="'+i+'">'+dataObject[i].title+'</option>'; 
    }; 

    // populate our select with newly received data and by using chain, bind "onChange" event to it: 
    $('#'+langSelect).html(langs).change(function() { 
     // if selected the empty row, die. 
     if (!$(this).val()) return false; 

     // remove high light CSS class from the active select box 
     $('#'+langSelect).removeClass('highlighted'); 

     var val = $(this).val(); // value we got from selected option, f.e: "EN" (English EN) 

     // populate status select 
     dMgr.populateStatus(val); // passing the selected language 

     // if we change language, make sure we empty the last select - questions 
     // we pass only third argument as true which means - yes, erase the data 
     dMgr.populateQuestions(false,false,true); 

    }).addClass('highlighted'); // high light the active select box with CSS class 
}, 
populateStatus: function(lang) { 
    // if no language passed, die. 
    if (!lang) return false; 

    // if there's no status for select language - die. 
    if (dataObject[lang].status.length <= 0) return false; 

    // just a shortcut to the object statuses 
    var sObj = dataObject[lang].status; 

    // create select options string with its current content <only first one> (the "select status.." option) 
    var statuses = '<option value="">'+$('#'+statusSelect+' option:first').html()+'</option>'; 

    // loop through all statuses and get their titles and values for new options 
    for(i in sObj) { 
     statuses += '<option value="'+i+'">'+sObj[i].title+'</option>'; 
    }; 

    // populate our select with newly received data and by using chain, bind "onChange" event to it: 
    $('#'+statusSelect).html(statuses).change(function() { 
     // if selected the empty row, die. 
     if (!$(this).val()) return false; 

     // remove high light CSS class from the active select box 
     $('#'+statusSelect).removeClass('highlighted'); 

     var val = $(this).val(); // value we got from selected option, f.e: "1" (Answered) 

     // populate status select 
     dMgr.populateQuestions(lang, val); // passing the selected language   

    }).addClass('highlighted'); // high light the active select box with CSS class 
}, 
populateQuestions: function(lang, status, eraseData) { 
    if ((!lang || !status) && !eraseData) return false; 

    // We do this actions before checking everything else, as there might be eraseData option 
    // create select options string with its current content <only first one> (the "select question.." option) 
    var questions = '<option value="">'+$('#'+questionSelect+' option:first').html()+'</option>'; 

    // If we just erasing the data, then put what we already harvested back and stop. 
    if (eraseData) { 
     $('#'+questionSelect).html(questions); 
     return true; 
    }; 

    // if there's no status for select language - die. 
    if (dataObject[lang].status[status].questions.length <= 0) return false; 

    // shortcut to the questions 
    var sObj = dataObject[lang].status[status].questions; 

    // loop through all statuses and get their titles and values for new options 
    for(i in sObj) { 
     for(key in sObj[i]) { 
      questions += '<option value="'+key+'">'+sObj[i][key]+'</option>'; 
     }; 
    }; 

    // populate our select with newly received data and by using chain, bind "onChange" event to it: 
    $('#'+questionSelect).html(questions).change(function() { 
     // if selected the empty row, die. 
     if (!$(this).val()) return false; 

     // remove high light CSS class from the active select box 
     $('#'+questionSelect).removeClass('highlighted'); 

     var val = $(this).val(); // value we got from selected option, f.e: "1" (Answered) 

     /* 
     * Do whatever you want here. "val" in this scope is the latest value selected (question) 
     * Either build more selects, or do AJAX call, or change page context. 
     */ 
     alert('You\'ve reached the last select option and selected it, what should I do now?'); 

    }).addClass('highlighted'); // high light the active select box with CSS class 
} 
}; 

:E全体の機能は、これはあなたの「main.js」ファイルであります:p

ボラ!

0

単純な "クライアント"依存ドロップダウンリスト(依存ドロップダウンリストの表示と翻訳を含む)。必要なajax呼び出しはありません。次の例では、マスターID「2」のみが詳細に示されています。 id "2"と同じ方法で他のマスターIDを追加します。あなたのMVCのビュー内

// Create the "master" dropdownlist 
    <div class="form-group"> 
     @Html.LabelForRequired(model => model.MasterDropdownId, @MyAPP.Resources.DefaultLanguage.MasterDropdown, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(model => model.MasterDropdownId, new SelectList(
       new List<Object>{ 
        new { value = "1", text = @MyAPP.Resources.DefaultLanguage.MasterDropdown_1}, 
        new { value = "2", text = @MyAPP.Resources.DefaultLanguage.MasterDropdown_2}, 
        new { value = "3", text = @MyAPP.Resources.DefaultLanguage.MasterDropdown_3}, 
        new { value = "4", text = @MyAPP.Resources.DefaultLanguage.MasterDropdown_4}, 
        new { value = "5", text = @MyAPP.Resources.DefaultLanguage.MasterDropdown_5}, 
        new { value = "6", text = @MyAPP.Resources.DefaultLanguage.MasterDropdown_6} 
          }, 
       "value", 
       "text", 
       ""), WIPRO.Resources.DefaultLanguage.Select_Value, new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.MasterDropdownId, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    // Save the initial “slave” id via a hidden field to be used after having populated the "slave" dropdownlist for the first time 
    @Html.Hidden("Initial_SlaveId", Model.SlaveDropdownId); 

    // Load all " slave ids" dropdownlist translated items 
    @Html.Hidden("Empty_text", @MyAPP.Resources.DefaultLanguage.Select_Value); 

    @Html.Hidden("Text_200", @MyAPP.Resources.DefaultLanguage.SlaveDropdown_200); 
    @Html.Hidden("Text_201", @MyAPP.Resources.DefaultLanguage.SlaveDropdown_201); 

    // Create the “slave" dropdownlist 
    <div id ="Div_SlaveDropdownId" class="form-group"> 
     @Html.LabelForRequired(model => model.SlaveDropdownId, @MyAPP.Resources.DefaultLanguage.SlaveDropdown, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(model => model.SlaveDropdownId, new SelectList(
       new List<Object>{ }, "value", "text", ""), new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.SlaveDropdownId, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <script> 

     // Populate " slave " dropdownlist based on the chosen type 
     function DisplaySpecificListItems(chosen_type) { 

      // Remove all items in the " slave " dropdownlist 
      $("#CurrentAccountLine_SlaveDropdownId > option").remove(); 

      // Add the default "empty" text item 
      var opt = document.createElement("option"); opt.text = $('#Empty_text').val(); opt.value = ""; document.getElementById("CurrentAccountLine_SlaveDropdownId").options.add(opt); 

      // Add the specific list items and show the " slave " dropdownlist 
      switch(chosen_type) { 

       case "2": 

        var opt = document.createElement("option"); opt.text = $('#Text_200').val(); opt.value = "200"; document.getElementById("CurrentAccountLine_SlaveDropdownId").options.add(opt); 
        var opt = document.createElement("option"); opt.text = $('#Text_201').val(); opt.value = "201"; document.getElementById("CurrentAccountLine_SlaveDropdownId").options.add(opt); 

        $("#Div_SlaveDropdownId").show(); 

        break; 

       default: 

        $("#Div_SlaveDropdownId").hide(); 

      } 

     } 

     // Initialize chosen_type with the initial “master” id 
     var chosen_type = $("#CurrentAccountLine_MasterDropdownId").val(); 

     // Initial loading of the "slave" dropdownlist items 
     DisplaySpecificListItems(chosen_type); 

     // Set "slave" dropdownlist selected items 
     $("#CurrentAccountLine_SlaveDropdownId").val($("#Initial_SubTypeId").val()); 

     // On a "master" dropdownlist change, repopulate the "slave" dropdownlist 
     $('#CurrentAccountLine_MasterDropdownId').change(function() { 

      // Update "chosen_type" 
      chosen_type = $("#CurrentAccountLine_MasterDropdownId").val(); 

      // Repopulate 
      DisplaySpecificListItems(chosen_type); 

     }); 

    </script> 

はそれが;-)

を役に立てば幸い
関連する問題