简体   繁体   中英

JQuery - onclick and .click handlers on element with same ID

how can I solve this? At the end of order proces on confirmation page I have form of payment method (could be various, the button id is the same) After click on confirm order button I need to call some_script.php using jquery and simultaneously redirect to payment gateway. After click on confirm button only onclick="$('#payment').submit(); is handled, page is redirected but some_script.php is not called. Thanks for help.

<div class="payment">
  <form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="payment">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    ....
  </form>
  <div class="buttons">
    <div class="right">
      <a id="button-confirm" class="button" onclick="$('#payment').submit();">
      <span>Confirm</span></a>
    </div>
  </div>
</div>

<script>
$('#button-confirm').click(function() {
$.get(
   'some_script.php',
   {order_id: '<?php echo $order_id; ?>'},
   'html'
    );
});
</script>

this maybe helful

<script>
    $('#button-confirm').click(function (e) {
        e.preventDefault();

        $.get(
           'some_script.php',
           {order_id: '<?php echo $order_id; ?>'},
           'html'
        );

        $('#payment').submit();
    });
</script>

is this what you looking for?


edit :
replace submit section on script with this, i think it's worked

var submitForm = $('#payment');
if(submitForm){
    $('#payment').submit();
}

edit2:
if there is various id for form tag, better use parent class and form selector to select the form

var submitForm = $('.payment form');
if(submitForm){
    $('.payment form').submit();
}

This is because you have written 2 different events for the same action. onclick="$('#payment').submit(); is handled because its written an inline.

For getting your expected result you mus change the code as below.

   <a id="button-confirm" class="button">

and

 <script>
 $('#button-confirm').click(function() {
 $.get(
'some_script.php',
{order_id: '<?php echo $order_id; ?>'},
'html'
 );
$('#payment').submit();
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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