从组合中选择特定选项时自动隐藏文本框

时间:2012-02-21 08:14:02

标签: javascript jquery html json

我有一个组合框,当选择第二个或第三个选项时显示一个文本框,但是当选择第一个选项时如何隐藏该文本框?

$("#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());

2 个答案:

答案 0 :(得分:1)

你真的需要服务器端吗?这里有一个没有的例子:

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

另见this example

但如果确实需要服务器端,则必须有条件地设置isTrue

答案 1 :(得分:0)

以下是隐藏的代码

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