简体   繁体   English

使用Jquery Issue提交表单

[英]Submitting form using Jquery Issue

I am using Jquery to submit a form. 我正在使用Jquery提交表单。 My code looks like this. 我的代码如下所示。

<?php if(isset($_POST['submit'])){
  echo $_POST['value'];
} else { ?>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script type="text/javascript" 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCV9b6j5csMVrafZXp6uTHOavxiJk4vqK4"></script>

  <form method="POST" action="" id="form1" >
    <input type="text" id="value" name="value">
    <button name="submit" onclick="tushar(); return false;">
      Click Me
    </button>

  </form>
  <script type="text/javascript">
    var tushar = function(){
      var geocoder = new google.maps.Geocoder();
      var address = "Hostel-7, IIT Bombay, Powai, Mumbai, Maharashtra,400076";
      geocoder.geocode({ 'address': address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          $("#value").val("POINT(");
          $('form#form1').submit();  
        } else {
          console.log(status);
          return false;
        }

      });       
    }
  </script>
<?php } ?>

On clicking the button, the value of input field is changed but the form is not being submitted. 单击该按钮时,输入字段的值将更改,但未提交表单。 Can anyone help me with this? 谁能帮我这个? Thanks in advance. 提前致谢。

Change the button name from submit to btnSubmit 将按钮名称从Submit更改为btnSubmit

<button name="btnSubmit" onclick="tushar(); return false;">Click Me</button>

and submitting should magically work when you stop overriding the method. 并且当您停止重写方法时,提交应该会神奇地起作用。

Please change $('form#form1').submit(); 请更改$('form#form1').submit(); to $('#form1')[0].submit(); $('#form1')[0].submit();

$('form#form1').submit(); is simply re-triggering the submit event on the form. 只是重新触发表单上的submit事件。

$('form#form1')[0].submit(); is used for submitting the form the default way. 用于以默认方式提交表单。 Note: an ID is usually enough to select an element hence $('#form1') . 注意:ID通常足以选择一个元素,因此可以选择$('#form1')

Further 进一步

As noted by @epascarello, you also need to change the name of your submit button so that it does not conflict with the event. 如@epascarello所述,您还需要更改提交按钮的名称,以使其与事件不冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM