当从组合框中选择特定选项时自动隐藏文本框
问题描述:
我有一个组合框,当选择第二或第三选项时显示文本框,但是当第一个选项被选中时如何隐藏该文本框?当从组合框中选择特定选项时自动隐藏文本框
$("#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());
答
你真的需要服务器端?此处没有示例:
$("#combo").change(function() {
if (this.value != '_') {
$("#textbox").empty().append("<input type='text' id='text1'/>");
}
else {
$("#textbox").hide();
}
});
另请参阅this example。
但是,如果确实需要服务器端,则必须有条件地设置isTrue
。
数据“应该”是一个基于您的内容类型的json对象它可能不是?在if语句上方的返回函数中尝试console.log(data)(如果它只返回一个字符串),您可能需要使用jQuery.parseJSON(data)才能像对象一样访问响应。 – 2012-02-21 08:24:19
@j_mcnally米错在这里:http://stackoverflow.com/questions/9377544/returning-sum-from-server-side-by-json – Phillipa 2012-02-21 12:30:54