2017-02-05 9 views
0

私のコードには、2つのオプションのリンゴと1つの親選択タグがあります。リンゴを選択すると、各オプションに1と2の値があります。別の選択タグが表示され、その逆もあります。別の選択と別の、私は別の選択を隠す必要があり、別の選択を表示する必要がありますあなたは私を助けることができますか?ここで selectタグを使用してselectタグを表示/非表示にする方法は?

はコードです:

$(document).ready(function() { 
 

 
    $('#defList').on('change', function() { 
 
    var tarSelect = $(this).val(); 
 
    console.log(tarSelect); 
 
    if (tarSelect == 1) { 
 
     var arrayList = [{ 
 
     val: 1, 
 
     text: 'one' 
 
     }, { 
 
     val: 2, 
 
     text: 'two' 
 
     }, { 
 
     val: 3, 
 
     text: 'three' 
 
     }]; 
 

 
     var selectList = $('<select>').appendTo('#selectBox'); 
 
     $(arrayList).each(function() { 
 
     selectList.append($('<option>').attr('value', this.val).text(this.text)); 
 
     }); 
 

 
    } else if (tarSelect == 2) { 
 
     var arrayList2 = [{ 
 
     val: 1, 
 
     text: 'one' 
 
     }, { 
 
     val: 2, 
 
     text: 'two' 
 
     }, { 
 
     val: 3, 
 
     text: 'three' 
 
     }]; 
 

 

 
     var selectList2 = $('<select>').appendTo('#selectBox').remove(); 
 
     $(arrayList2).each(function() { 
 
     selectList2.append($('<option>').attr('value', this.val).text(this.text)); 
 
     }); 
 

 
    } 
 

 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id="defList"> 
 
    <option value="1">Apples</option> 
 
    <option value="2">Butter</option> 
 
</select> 
 

 
<div id="selectBox"> 
 
</div>

+0

var selectList2 = $('<select>').appendTo('#selectBox').remove();を変更することができ、私は非表示にする必要があります別のものを選択し、別のものを表示する 'は、別の? – Viney

+0

配列で宣言された新しい選択リストが作成されます。 – Jemai

答えて

1

が#selectBoxのコンテンツを削除する最初のdrowpdown値が変更された

$(document).ready(function() { 
 

 
    
 
     function createDropdown(arrayList){ 
 
      
 
      var $select = $('<select>'); 
 
\t \t \t $(arrayList).each(function() { 
 
\t \t \t $select.append($('<option>').attr('value', this.val).text(this.text)); \t \t 
 
\t \t \t }); 
 
     return $select; 
 
     } 
 
    
 
\t \t $('#defList').on('change', function(){ 
 
      $("#selectBox").empty(); 
 
\t \t \t var tarSelect = $(this).val(); 
 
\t \t \t console.log(tarSelect); 
 
\t \t \t if(tarSelect == 1) { \t \t 
 
\t \t \t var arrayList = 
 
\t \t \t [ 
 
\t \t \t {val: 1,text: 'one'}, 
 
\t \t \t {val: 2,text: 'two'}, 
 
\t \t \t {val: 3,text: 'three'} 
 
\t \t \t ]; 
 
      createDropdown(arrayList).appendTo('#selectBox'); 
 
\t \t 
 
\t \t } else if (tarSelect == 2) { 
 
\t \t \t var arrayList2 = 
 
\t \t \t [ 
 
\t \t \t {val: 1,text: 'one'}, 
 
\t \t \t {val: 2,text: 'two'}, 
 
\t \t \t {val: 3,text: 'three'} 
 
\t \t \t ]; 
 
\t \t \t 
 
\t \t \t createDropdown(arrayList2).appendTo('#selectBox'); \t \t 
 
\t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t }); \t 
 
    $('#defList').trigger("change"); 
 
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
 
    "http://www.w3.org/TR/html4/strict.dtd"> 
 
<HTML> 
 
    <HEAD> 
 
     <TITLE>My first HTML document</TITLE> 
 
    </HEAD> 
 
    <BODY> 
 
     <P>Hello world!</p> 
 
\t \t 
 
\t \t <select id="defList"> 
 
\t \t \t <option value="1">Apples</option> 
 
\t \t \t <option value="2">Butter</option> 
 
\t \t </select> 
 
\t 
 

 
\t \t <div id="selectBox"> 
 
\t 
 
\t \t 
 
\t \t </div> 
 
\t 
 
\t \t 
 
\t 
 
\t <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
\t <script src="js/scripts.js"></script> 
 
    </BODY> 
 
</HTML>

+0

はい、動作しますが、デフォルトの選択タグを表示することは可能ですか? – Jemai

+0

はい、変更イベントをトリガーする必要があります(スニペットを更新しました) –

+0

ドキュメントの読み込み時にトリガー()が開始されるように機能しましたか? – Jemai

1

あなただけ$('#selectBox').html("");

を使用して、最初は新しい選択領域をクリアししかし、また `私の問題は、それが別の選択と別のものを表示しますですvar selectList2 = $('<select>').appendTo('#selectBox');

$(document).ready(function() { 
 

 
\t $('#defList').on('change', function() { 
 
     $('#selectBox').html(""); 
 
\t \t var tarSelect = $(this).val(); 
 
\t \t console.log(tarSelect); 
 
\t \t if (tarSelect == 1) { 
 
\t \t \t var arrayList = [{ 
 
\t \t \t \t \t val: 1, 
 
\t \t \t \t \t text: 'A one' 
 
\t \t \t \t }, 
 
\t \t \t \t { 
 
\t \t \t \t \t val: 2, 
 
\t \t \t \t \t text: 'A two' 
 
\t \t \t \t }, 
 
\t \t \t \t { 
 
\t \t \t \t \t val: 3, 
 
\t \t \t \t \t text: 'A three' 
 
\t \t \t \t } 
 
\t \t \t ]; 
 
       
 
      
 
\t \t \t var selectList = $('<select>').appendTo('#selectBox'); 
 
\t \t \t $(arrayList).each(function() { 
 
\t \t \t \t selectList.append($('<option>').attr('value', this.val).text(this.text)); 
 
\t \t \t }); 
 

 
\t \t } else if (tarSelect == 2) { 
 
\t \t \t var arrayList2 = [{ 
 
\t \t \t \t \t val: 1, 
 
\t \t \t \t \t text: 'B one' 
 
\t \t \t \t }, 
 
\t \t \t \t { 
 
\t \t \t \t \t val: 2, 
 
\t \t \t \t \t text: 'B two' 
 
\t \t \t \t }, 
 
\t \t \t \t { 
 
\t \t \t \t \t val: 3, 
 
\t \t \t \t \t text: 'B three' 
 
\t \t \t \t } 
 
\t \t \t ]; 
 

 

 
\t \t \t var selectList2 = $('<select>').appendTo('#selectBox'); 
 
\t \t \t $(arrayList2).each(function() { 
 
\t \t \t \t selectList2.append($('<option>').attr('value', this.val).text(this.text)); 
 
\t \t \t }); 
 

 
\t \t } 
 

 
\t }); 
 
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
 
<HTML> 
 

 
<HEAD> 
 
\t <TITLE>My first HTML document</TITLE> 
 
</HEAD> 
 

 
<BODY> 
 
\t <P>Hello world!</p> 
 

 
\t <select id="defList"> 
 
\t \t <option value="1">Apples</option> 
 
\t \t <option value="2">Butter</option> 
 
\t </select> 
 

 
\t <div id="selectBox"> 
 

 
\t </div> 
 

 
\t <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
\t <script src="js/scripts.js"></script> 
 
</BODY> 
 

 
</HTML>

+0

それは、選択したタグを表示するリンゴを選択すると可能性がありますか? – Jemai

関連する問題