【发布时间】:2012-02-21 16:34:39
【问题描述】:
我有一个组合框,在选择第二个或第三个选项时显示一个文本框,但是选择第一个选项时如何隐藏该文本框?
$("#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());
【问题讨论】:
-
数据“应该”是基于您的内容类型的 json 对象,它可能不是吗?在 if 语句上方的 return 函数中尝试 console.log(data) 如果它只返回一个字符串,你可能需要 jQuery.parseJSON(data) 才能像对象一样访问响应。
-
@j_mcnally 我在这里错了:stackoverflow.com/questions/9377544/…
标签: javascript jquery html json