【问题标题】:editable textbox after receiving data by JSON通过 JSON 接收数据后可编辑的文本框
【发布时间】:2012-02-09 09:54:20
【问题描述】:

我通过 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”,但我无法再编辑此文本框,为什么?我没有将其设置为只读。任何帮助

【问题讨论】:

    标签: javascript jquery ajax json


    【解决方案1】:

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

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

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

    【讨论】:

    • 很高兴它有帮助。然后,您可以通过单击此答案旁边的复选标记来接受答案。
    • 是的,我接受它,但它告诉你再等 4 分钟,你能支持我的问题吗
    【解决方案2】:

    你应该这样做

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

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

    【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多