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

editable textbox after receiving data by JSON

本文关键字:编辑 文本 JSON 数据 通过      更新时间:2023-09-26

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

//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",但我不能再编辑这个文本框了,为什么?我没有把它设置为只读。任何帮助

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

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

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

您应该执行

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

否则,对于replaceWith(),您将用文本替换DOM元素,这就是为什么它是只读