JSON接收数据后的可编辑文本框

时间:2012-02-09 09:54:20

标签: javascript jquery ajax json

我正在通过JSON从state.jsp接收数据,并在具有id textbox2的文本框中的auto.jsp中显示数据。但我无法编辑我接收数据的文本框,为什么?

// 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 个答案:

答案 0 :(得分:2)

.replaceWith()用指定的值(text,dom元素,jquery对象)替换匹配的集合。因此,在您的代码中,您将使用响应数据替换while INPUT元素,而不是设置其值

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

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

答案 1 :(得分:2)

你应该做

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

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