2012-02-21 15 views
0

2番目または3番目のオプションを選択したときにテキストボックスを表示するコンボボックスは1つありますが、特定のオプションがコンボから選択されたときに自動的にテキストボックスが表示される

$("#combo").change(function() { 
     $.getJSON('combo.jsp', { 
      comboname: this.value 
     }, function (data) { 
      if(data.isTrue){ 
      $("#textbox").empty().append("<input type='text' id='text1'/>");// display an empty text box 
        } 
        else{ 
         // how to clear that text box and hide it? 
        } 
     }); 
    }); 

HTML

<select id="combo" name="comboname"> 
<option value="_"></option>// how to clear and hide the text box when this option is 
    selected? 
<option value="somevalue">somename</option>// when this option is selected then it 
displays an empty text box 
<option value="somevalue1">somename1</option>when this option is selected then it also 
displays the same empty text box 
</select> 
// in the following div, text box is being displayed 
<div id="textbox"> 
// here text box is displayed when option 2nd or 3rd is selected from the above combo 
</div> 

サーバ側(combo.jsp)

JSONObject jsonObj= new JSONObject(); 
jsonObj.put("isTrue","true"); 
response.setContentType("application/json"); 
response.getWriter().write(jsonObj.toString()); 
+0

データはコンテンツタイプに基づいてjsonオブジェクトにする必要がありますか? if文の上の返り値関数でconsole.log(data)を試してください。返り値が単なる文字列を返す場合は、オブジェクトのような応答にアクセスする前にjQuery.parseJSON(data)する必要があります。 –

+0

@j_mcnallyここでmiが間違っています:http://stackoverflow.com/questions/9377544/returning-sum-from-server-side-by-json – Phillipa

答えて

1

を隠すためのコードであるあなたが本当にサーバー側が必要ですか?以下の例はありません。

$("#combo").change(function() { 
    if (this.value != '_') { 
     $("#textbox").empty().append("<input type='text' id='text1'/>"); 
    } 
    else { 
     $("#textbox").hide(); 
    } 
}); 

this exampleも参照してください。

実際にサーバー側が必要な場合は、条件付きでisTrueを設定する必要があります。

+0

はい問題は解決しました! – Phillipa

+0

@ scessoreここで私は間違っています:http://stackoverflow.com/questions/9377544/returning-sum-from-server-side-by-json – Phillipa

0

$('#textbox').hide(); 
+0

それは動作しません。 – Phillipa

+0

@Phillipa:サーバー側あなたはいつも 'isTrue = true';クライアント側の 'if(data.isTrue)'も常に 'true'です。あなたは決して 'else'の部分に入ることはありません。 – scessor

+0

はい、サーバー側からのデータを処理するためにクライアント側に複数のifブロックを書く必要がありますか? – Phillipa

関連する問題