2012-02-21 137 views
0

我有一個組合框,當選擇第二或第三選項時顯示文本框,但是當第一個選項被選中時如何隱藏該文本框?當從組合框中選擇特定選項時自動隱藏文本框

$("#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)才能像對象一樣訪問響應。 – 2012-02-21 08:24:19

+0

@j_mcnally米錯在這裏:http://stackoverflow.com/questions/9377544/returning-sum-from-server-side-by-json – Phillipa 2012-02-21 12:30:54

回答

1

你真的需要服務器端?此處沒有示例:

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

另請參閱this example

但是,如果確實需要服務器端,則必須有條件地設置isTrue

+0

是問題解決了! – Phillipa 2012-02-21 08:38:09

+0

@ scessore在這裏我錯了在這裏:http://stackoverflow.com/questions/9377544/returning-sum-from-server-side-by-json – Phillipa 2012-02-21 12:25:32

0

是代碼隱藏

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

它不工作 – Phillipa 2012-02-21 08:18:32

+0

@Phillipa:服務器端,你總是用'isTrue回答= true';所以在客戶端'if(data.isTrue)'也總是'true';你永遠不會進入'else'部分。 – scessor 2012-02-21 08:23:32

+0

是的,然後我必須寫在客戶端處理數據從服務器端多塊如果塊? – Phillipa 2012-02-21 08:25:22

相關問題