2015-02-24 86 views
0

我正在嘗試使用Ajax從Servlet獲取一個值到JSP的輸入框。我已經設法將這個值傳遞給Alert框。但是,當我試圖將該值輸入到輸入框時,它不起作用。請參閱下面的代碼。使用Ajax和Servlet填充文本框

這是ajaxtTestServlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException 
    { 
    response.setContentType("text/html"); 

    String text = "some text"; 
    response.setCharacterEncoding("UTF-8"); 

    response.getWriter().write(text); 
} 

還有就是id爲 「名字」 的輸入框。

<button type="button" class="close" data-dismiss="modal" aria- 

hidden="true">&times;</button> 
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> <a href="#" data-dismiss="modal"> <img src="images/arrow-back-512.png" width="30px" height="30px"> <small>Back</small></a> <span id="myModalLabel" style="margin-left:20px;"><font size="+2"><b>Edit New Member</b></font></span> </div> 
     <div class="modal-body"> 
      <form class="form-horizontal" method="post" action="EditOnlySrvlt?idEmployee=<%=request.getParameter("idEmployee")%>"" > 
      <fieldset id="modal_form"> 

      <!-- Text input--> 
      <div class="form-group"> 
       <label class="col-md-4 control-label" for="textinput">First Name</label> 
       <div class="col-md-6"> 
       <input name="firstName" id="firstName" type="text" class="form-control input-md" > 
       </div> 
      </div> 

      <!-- Text input--> 
      <div class="form-group"> 
       <label class="col-md-4 control-label" for="textinput">Middle Name</label> 
       <div class="col-md-6"> 
       <input name="middleName" type="text" class="form-control input-md"> 
       </div> 

      </div> 

      <!-- Text input--> 
      <div class="form-group"> 
       <label class="col-md-4 control-label" for="textinput">Last Name</label> 
       <div class="col-md-6"> 
       <input name="lastName" type="text" class="form-control input-md"> 
       </div> 

      </div> 

      <!-- Text input--> 
      <div class="form-group"> 
       <label class="col-md-4 control-label" for="textinput">Date Of Birth</label> 
       <div class="col-md-6"> 
       <input name="dateOfBirth" type="date" class="form-control input-md"> 
       </div> 

      </div> 

      <!-- Text input--> 
      <div class="form-group"> 
       <label class="col-md-4 control-label" for="textinput">Passport Number</label> 
       <div class="col-md-6"> 
       <input name="passportNumber" type="text" class="form-control input-md"> 
       </div> 

      </div> 



     <div class="modal-footer"> 
     <button type="submit" class="btn btn-primary">Edit</button> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
     </div> 
       </fieldset> 
     </form> 

    </div> 
    </div> 
</div> 
</div> 

<!-- /Edit Job modal --> 

這是腳本

<script> 
$(function() { 
//twitter bootstrap script 
$("tr#somebutton").click(function(){ 

$.get("ajaxTestSrvlt", function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text... 
    $('#firstName').text(responseText) ;  // Locate HTML DOM element with ID "somediv" and set its text content with the response text. 
}); 

}); 
}); 
</script> 

回答

0

如果它是一個輸入,你應該給

$('#firstName').val(responseText); 
+0

我試過了。沒有工作 – TheShadow123 2015-02-24 10:40:55

+0

嘗試$('#firstName').val(JSON.stringify(responseText)); – Ila 2015-02-24 10:43:47

+0

仍然一樣。 – TheShadow123 2015-02-24 10:49:40