【问题标题】:Submitting a form by clicking a radio button, then running ajax通过单击单选按钮提交表单,然后运行 ​​ajax
【发布时间】:2014-10-20 02:35:03
【问题描述】:

我有一个结帐表格,它适用于除 IE8 之外的所有应用程序。 我正在更改它以使用 ajax 将输入结果保存为会话数据。 当我使用第一个“点击”功能时,一切都很好,但我无法触发“提交”功能(没有警报)。以下显示/隐藏正在工作。 请让我另眼相看。

表格

<form name="payment_method_form" id="payment_method_form" method="post">
  <div class="pay_method">
    <input type="radio" name="pay_method" value="cc"> Credit Card
  </div>
  <div class="pay_method">
    <input type="radio" name="pay_method" value="pp"> Paypal
  </div>
  <div class="pay_method">
    <input type="radio" name="pay_method" value="dd"> Direct Deposit
  </div>
  <div class="pay_method">
    <input type="radio" name="pay_method" value="cash"> Cash on Delivery
  </div>
</form>

脚本(包含在 $(document).ready() 中)

$('input[name$="pay_method"]').click(function() {
        $('#payment_method_form').submit(function(e) {
            alert("clicked")
            <!-- ajax here -->
        });
    var pay_method = $(this).val();
    $('.pay_option').hide();
    $("#"+pay_method).show();

});

【问题讨论】:

    标签: jquery ajax radio-button form-submit


    【解决方案1】:

    submit 事件侦听器必须在click 事件侦听器之外,并且您想触发click 事件中的submit 事件,如下所示:

    $('input[name$="pay_method"]').click(function() {
        //...
        $('#payment_method_form').submit(); //TRIGGER FORM SUBMIT EVENT
    });
    $('#payment_method_form').submit(function(e) { //START - Submit event listener
        e.preventDefault();
        alert("clicked")
    });
    

    $('input[name$="pay_method"]').click(function() {
      var pay_method = $(this).val();
      $('.pay_option').hide();
      $("#" + pay_method).show();
      $('#payment_method_form').submit();
    });
    $('#payment_method_form').submit(function(e) {
      e.preventDefault();
      alert("clicked")
        //<!-- ajax here -->
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form name="payment_method_form" id="payment_method_form" method="post">
      <div class="pay_method">
        <input type="radio" name="pay_method" value="cc">Credit Card
      </div>
      <div class="pay_method">
        <input type="radio" name="pay_method" value="pp">Paypal
      </div>
      <div class="pay_method">
        <input type="radio" name="pay_method" value="dd">Direct Deposit
      </div>
      <div class="pay_method">
        <input type="radio" name="pay_method" value="cash">Cash on Delivery
      </div>
    </form>

    【讨论】:

    • 辛苦了,非常感谢。
    • 太棒了!很高兴我能帮上忙。
    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多