2012-02-21 133 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

相关问题