ajax提交表单后,清空表单

<div align="center" id="div2">
    <form>
    商品名称:<input type="text" id="gname"><br><br>
    商品价格:<input type="text" id="gprice"><br><br>
    商品种类:<input type="text" id="category"><br><br>
    <input type="button" value="添加" id="but2">
    <input type="reset" style="display:none;" /> 
    </form>

    </div>

<script type="text/javascript">
 
  $("#but2").click(function(){
  var gname = $("#gname").val();
  var gprice = $("#gprice").val();
  var catagory = $("#category").val();
  $.ajax({
  url:"goodsAction_addGoods.action",
  type:"post",
  data:{"goods.GName":gname,"goods.GPrice":gprice,"goods.GSpecies":catagory},
  success:function(result){
  alert(result);
  $("table").html("");
  var htmls = "<tr><td>商品编号</td><td>商品名称</td><td>商品价格</td><td>商品种类</td><td>操作</td></tr>";
  $.each(result,function(i,n){
  htmls += "<tr><td>"+n.GId+"</td><td>"+n.GName+"</td><td>"+n.GPrice+"</td><td>"+n.GSpecies+"</td><td><a href='shoppingAction_addOrder.action?GId="+n.GId+"'>添加到购物车</a></td></tr>";
  });
  $("table").html(htmls);
  $("#div2").hide();
  $("input[type=reset]").trigger("click");//触发reset
  }
 
  });
  });
 
  </script>


已标记关键词 清除标记
<div class="post-text" itemprop="text"> <p>I have a small contact form. Sending messages is working but the form stays with text after submitting. I searched and tried some code to clear it with no success.</p> <pre><code>// JavaScript Document $('#contact-form').submit(function (e) { "use strict"; e.preventDefault(); $.ajax({ type: "POST", url: "contactform.php", data: $(this).serialize(), //get form values sucess: function (data) { document.getElementById("contact-form").reset(); } }).done(function (data) { console.log(data); // will contain success or invalid }); }); </code></pre> <p>And some php</p> <pre><code><?php $firstname = $_POST["firstname"]; $lastname = $_POST["lastname"]; $email = $_POST["email"]; $phone = $_POST["phone"]; $message = $_POST["message"]; $EmailTo = "marcin@rmonline.com"; $Subject = "New Message Received"; // prepare email body text $email_content .= "Firstname: $firstname "; $email_content .= "Lastname: $lastname "; $email_content .= "Email: $email "; $email_content .= "Phone: $phone "; $email_content .= "Message: $message"; // send email $success = mail($EmailTo, $Subject, $email_content, "From:".$email); // redirect to success page if ($success){ echo "success"; } else { echo "invalid"; } ?> </code></pre> <p>And html</p> <pre><code><form id="contact-form" method="post" action="contactform.php" role="form"> <div class="messages"></div> <div class="form-group"> <input id="form_name" type="text" name="firstname" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required."> <div class="help-block with-errors"></div> </div> <div class="form-group"> <input id="form_lastname" type="text" name="lastname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required."> <div class="help-block with-errors"></div> </div> <div class="form-group"> <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required."> <div class="help-block with-errors"></div> </div> <div class="form-group"> <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone"> <div class="help-block with-errors"></div> </div> <div class="form-group"> <textarea id="form_message" name="message" class="form-control" placeholder="Message for us *" rows="4" required data-error="Please,leave us a message."></textarea> <div class="help-block with-errors"></div> </div> <button type="submit" class="btn" value="Send message">Send Message</button> <p class="text-muted"><strong>*</strong> These fields are required.</p> </form> </code></pre> <p>I've got the feeling that I'm missing something, but don't know what yet.</p> </div>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页