使用Ajax和Servlet填充文本框

时间:2015-02-24 10:34:02

标签: ajax jsp servlets

我试图使用Ajax从Servlet获取值到JSP的输入框。我已设法将该值传递给Alert框。但是当我试图将该值放入输入框时,它不起作用。请参阅以下代码。

这是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为“firstName”的输入框在哪里。

<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>  

1 个答案:

答案 0 :(得分:0)

如果是输入,则应该提供

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