2012-02-09 94 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