2012-02-09 73 views
3
接收数据后

我通过JSON在auto.jsp在具有ID文本框接收来自state.jsp数据和显示数据textbox2.But我不能够编辑文本框我在哪里接收数据,为什么?可编辑文本框通过JSON

// auto.jsp

$("#combo1").change(function() { 
    // by onchange event of combobox, i am displaying string "anyname" 
    // on that below textbox. 
    $.getJSON('state.jsp', { combo1Val : $(this).val() }, function(responsedata) { 
     $("#textbox2").replaceWith(responsedata.name); 
    }); 
}); 
// i am displaying "anyname" here, but why i am not able 
// to edit this text box after displaying? I have not set it to readonly 
<input type="text" id="textbox2" name="2ndtextbox/> 

// state.jsp

<%@page import="net.sf.json.JSONObject"%> 
<%@page import="net.sf.json.JSONArray"%> 
<% 
JSONObject arrayObj= new JSONObject(); 
     arrayObj.put("name","anyname");// displaying "anyname" in that textbox 
     response.setContentType("application/json"); 
     response.getWriter().write(arrayObj.toString()); 
%> 

我在文本框中显示字符串 “anyname” 但我不能够为什么编辑这个文本框?我没有把它设置为只读。任何帮助

回答

2

.replaceWith()替换匹配的由指定的值(文字,DOM元素,一个jquery对象)来设置。因此,在你的代码,你与你的响应数据替换,而INPUT元素,而不是它的值设置

要设置表单元素的值,使用.val()方法:

$("#textbox2").val(responsedata.name); 
+0

非常感谢它的工作 – harry 2012-02-09 10:01:25

+0

很高兴帮助。您可以通过点击此答案旁边的复选标记来接受安检。 – 2012-02-09 10:04:13

+0

亚我很接受它,但它是在告诉等待4分钟以上,可以请你给予好评我的问题 – harry 2012-02-09 10:06:36

2

你应该做的

$("#textbox2").val(responsedata.name); 

否则与replaceWith()你与你的文字替换的DOM元素,这就是为什么它是只读

+0

非常感谢它的工作 – harry 2012-02-09 10:02:09

+0

请看看这个:HTTP ://stackoverflow.com/questions/9238267/clean-old-options-from-child-combo-box-when-receiving-data-by-json-array-objects – harry 2012-02-11 10:02:23

+0

请[见这个问题(HTTP:// stackoverflow.com/questions/9248383/retrieve-more-than-one-value-by-json-array-objects)尼古拉,如果可能的话,请提出一些想法 – harry 2012-02-13 04:01:03