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

标签 javascript jquery ajax json

我正在通过 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/> 

//状态.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()用指定的值(文本、dom 元素、jquery 对象)替换匹配的集合。因此,在您的代码中,您将 while INPUT 元素替换为您的响应数据,而不是设置其值

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

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

关于javascript - 通过 JSON 接收数据后的可编辑文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9208610/

相关文章:

javascript - Jquery 在 ajaxComplete 上调用相同的函数并准备好了吗?

.net - 动态添加客户端脚本/HTML 调用服务器端事件

javascript - 寻找设计资源来练习 HTML/CSS

javascript - 如何使用 fetch() 从请求中获取响应的内容长度

javascript - 表单未在 javascript 中验证

jquery - DatePicker 无法在 ajax 加载的页面中工作

javascript - jqGrid 如何操作 : Get the value of specific cell upon double click on row

javascript - 将数据库中的图像附加到 jQuery 中的 div 中

javascript - 使用 AJAX 回显表单提交到 div

javascript - 如何在包含其他内容的字符串中查找电话号码?