是否可以在JAVASCRIPT中调用servlet并从中获取值

时间:2015-08-20 12:07:49

标签: javascript jquery jsp servlets

我有一个表单,我希望在select change事件上更改文本字段的值,我想调用select字段的onchange事件来调用从数据库获取值的javascript函数,但它不起作用,需要根据这种情况可行的例子(我不熟悉AJAX / JSON / JSTL等,所以我没有使用它们)。

       (NEED WORKABLE EXAMPLE)

          -----------------------------
   Name   |                           |   (Select Field)
          -----------------------------

          -----------------------------
   Price  |                           |   (Text Field)
          -----------------------------

          -----------------------------
   Image  |                           |
          |                           |
          |                           |
          |                           |
          |                           |
          -----------------------------

          <script>

            function updateFields()
            {
              <% ProductDAO yy = new ProductDAO();
              String q = request.getParameter("orproductname");
              String f = yy.getProductPrice(q);
              request.setAttribute("price", f);
              %>

              document.getElementById("orproprice").value = f;
            }

          </script>

                <form name="product" method="post" action=".\GetProducts">
                        <div class= "form">
                            <div class="input-group margin-bottom-sm">
                                <span class="input-group-addon">
                                    <i class="fa fa-bars"></i>
                                </span>
                                <select class="form-control" name="orproductname" id="orproname" onchange="document.product.submit();">
                                <% ProductDAO pdr = new ProductDAO(); String[] kl = pdr.getProductNames();
                                for(int i=0;i<kl.length;i++) {%>
                                <option><% out.println(kl[i]); %></option>
                                <% } %>
                                </select>
                            </div>
                            <div class="input-group margin-bottom-sm">
                                <span class="input-group-addon">
                                    <i class="fa fa-bars"></i>
                                </span>
                                <input class="form-control" type="text" placeholder="Original Price" id="orproprice" name="orproductprice" required>
                            </div>
                            <div class="input-group margin-bottom-sm">
                                <span class="input-group-addon">
                                    <i class="fa fa-picture-o"></i>
                                </span>
                                <img class="img-responsive" src="img/game-2.jpg" alt="Product" >
                            </div>

                        <input class="btn btn-primary send" style="width:200px; margin-right:20px" type="submit" value="Order">
                        <input class="btn btn-primary send" style="width:200px" type="reset" value="Clear">
                    </form>
                </div>  
            </div>

1 个答案:

答案 0 :(得分:0)

.\GetProducts发送POST AJAX请求。请参阅AJAX的jQuery文档:http://api.jquery.com/jquery.ajax/。例如:

$.ajax({
  method: "POST",
  url: "/GetProducrs",
  data: { formField: value, ... }
}).done(function( data ) {
    //process data
});