代码中存在问题,我无法使用AJAX接收它

时间:2019-05-03 13:35:03

标签: jquery ajax servlets

我有一个将产品添加到该类别的表格,这是通过使用AJAX使用servlet插入数据库来完成的,我的表格中有6个组件,它们接受名称,价格,数量,图像(图像名称),类别和描述 当我填写说明时归档from时,它将所有值发送到servlet,而无需单击添加Product按钮

这是我的整个表格:-

<div class="p-t-33 p-b-60">
  <div class="flex-m flex-w p-b-10">
    <div class="s-text15 w-size15 t-center">
      <b>Product Image-</b>
    </div>
    <div class="rs2-select2 rs3-select2 bo4 of-hidden w-size16">
      <input type="file" id="fileclear" name="productimage">
    </div>
  </div>
  <div class="flex-m flex-w p-b-10">
    <div class="s-text15 w-size15 t-center">
      <b>Product Name-</b>
    </div>
    <div class="rs2-select2 rs3-select2 bo4 of-hidden w-size16">
      <input type="text" name="productname" id="productname">
    </div>
  </div>
  <div class="flex-m flex-w p-b-10">
    <div class="s-text15 w-size15 t-center">
      <b>Product Price-</b>
    </div>
    <div class="rs2-select2 rs3-select2 bo4 of-hidden w-size16">
      <input type="text" name="productprice" id="productprice">
    </div>
  </div>
  <div class="flex-m flex-w p-b-10">
    <div class="s-text15 w-size15 t-center">
      <b>Product Quantity-</b>
    </div>
    <div class="rs2-select2 rs3-select2 bo4 of-hidden w-size16">
      <input type="text" name="productquantity" id="productquantity">
    </div>
    /div>
    <div class="flex-m flex-w p-b-10">
      <div class="s-text15 w-size15 t-center">
        <b>Product Description-</b>
      </div>
      <div class="form-group">
        <textarea class="form-control" rows="7" id="ogdescription" name="description"></textarea>
      </div>
    </div>
    <div class="flex-m flex-w">
      <div class="s-text15 w-size15 t-center">
        <b>Category name-</b>
      </div>
      <input type="text" name="categorynameproduct" class="getcat">
      <br>
      <div class="anyclass text-center">
        <div class="list-group" id="mainlist">
          <b><a id="mainofmainlist" class="list-group-item"></a></b>
        </div>
      </div>
    </div>
    <br>
  </div>
  <div class="flex-r-m flex-w p-t-10">
    <div class="w-size16 flex-m flex-w">
      <div class="btn-addcart-product-detail size9 trans-0-4 m-t-10 m-b-10">
        <!-- Button -->
        <button name="addproduct" id="addproduct" class="flex-c-m sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4">
                 <b> Add Product</b>
                    </button>
      </div>
    </div>
  </div>
  <br>
  <span class="label label-success" id="messageshow" style="width: 80%;font-size: 28px;text-align: center;right:10%">HI GUYS</span>
</div>

这是我的AJAX代码

$('.anyclass').click(function(event) {
  var getit = event.target.innerHTML;
  $('.getcat').val(getit);
  return false;
});
var imagename, ogdescription;
$('input[type="file"]').change(function(e) {
  imagename = e.target.files[0].name;
  ogdescription = $('#ogdescription').val();
});
var go = 'looki';
$('#addproduct').click(function() {
  $.ajax({
    url: 'insertproduct',
    data: {
      productname: $('#productname').val(),
      productprice: $('#productprice').val(),
      productquantity: $('#productquantity').val(),
      catename: $('.getcat').val(),
      imagename: imagename,
      description: ogdescription
    },
    success: function(responseText) {
      if (responseText !== null) {
        $('#productname').val("");
        $('#productprice').val("");
        $('#productquantity').val("");
        $('.getcat').val("");
        $('#ogdescription').val("");
        $('#fileclear').val("");
        $('.label').show();

        $('#messageshow').text(responseText);

      }
    }
  });
});

我的代码中是否存在任何问题,该问题导致在单击按钮之前将数据放入servlet

0 个答案:

没有答案