使用jquery插件提交表单

时间:2023-01-15 11:36:49

I am trying to submit the form with jquery. I am using bootstrap modal window. Here is a js fiddle. Am i missing something? thanks alot

我试图用jquery提交表单。我正在使用bootstrap模态窗口。这是一个小提琴。我错过了什么吗?非常感谢

Update: I am trying to submit the form using ajax. I also tried but not luck.

更新:我正在尝试使用ajax提交表单。我也试过但没有运气。

$('#comment_form').on('submit', function(){

  $.post("/yourReceivingPage", $(this).serialize(), function(){

  // Hide the modal
    $("#my-modal").modal('hide');

  });

  // Stop the normal form submission
  return false;
});

3 个解决方案

#1


1  

You refer to the wrong element, I have a working example for you, please check and let me know if it works for you:

你提到错误的元素,我有一个工作的例子,请检查并让我知道它是否适合你:

$(document).ready(function() {
    $('#comment-form-submit').click(function() {
        $('#comment_form').submit();
        alert('Handler for .submit() called.');
        return false;
    });
});​

jsFiddle Working Demo

jsFiddle工作演示

And for AJAX solution you need to refer to familiar and already discussed issue:

对于AJAX解决方案,您需要参考熟悉且已经讨论过的问题:

jquery serialize and $.post

jquery序列化和$ .post

Edited: Referring to your question about how to extract the ID of the clickable link, this code will do it for you:

编辑:参考您关于如何提取可点击链接的ID的问题,此代码将为您执行:

 $(document).ready(function() {
   $(".comments.no").mouseover(function() {
     myDivsId = this.id;  // as you mouse over on the link it will be stored in Global Var and then transferred anywhere you wish.
   });
     $('#comment-form-submit').click(function() {
       $('#comment_form').submit();
       alert('Handler for .submit() called + div\'s ID = ' + myDivsId);
       return false;
   });

});​

jsFiddle live demo

jsFiddle现场演示

#2


0  

You need to add a click event for comment-form-submit button.

您需要为comment-form-submit按钮添加click事件。

$(document).on('click','#comment-form-submit', function() {
    $('#comment_form').submit(function() {
       alert('Handler for .submit() called.');
       return false;
    });​
});

#3


0  

You are looking for just submit(). What you are doing in your example is creating a function that will be run when the form is submitted. Also you are not setting up the handler for the clicking to submit the form.

您正在寻找提交()。您在示例中所做的是创建一个在提交表单时运行的函数。此外,您没有设置单击以提交表单的处理程序。

// This creates submit handler for the form
$('#comment_form').submit(function() {
    alert('Handler for .submit() called.');
    return false;
});​

// This creates the on click handler for the submit button
$('#comment-form-submit').on('click', function() {
    // This actually submits the form
    $('#comment_form').submit();
});

#1


1  

You refer to the wrong element, I have a working example for you, please check and let me know if it works for you:

你提到错误的元素,我有一个工作的例子,请检查并让我知道它是否适合你:

$(document).ready(function() {
    $('#comment-form-submit').click(function() {
        $('#comment_form').submit();
        alert('Handler for .submit() called.');
        return false;
    });
});​

jsFiddle Working Demo

jsFiddle工作演示

And for AJAX solution you need to refer to familiar and already discussed issue:

对于AJAX解决方案,您需要参考熟悉且已经讨论过的问题:

jquery serialize and $.post

jquery序列化和$ .post

Edited: Referring to your question about how to extract the ID of the clickable link, this code will do it for you:

编辑:参考您关于如何提取可点击链接的ID的问题,此代码将为您执行:

 $(document).ready(function() {
   $(".comments.no").mouseover(function() {
     myDivsId = this.id;  // as you mouse over on the link it will be stored in Global Var and then transferred anywhere you wish.
   });
     $('#comment-form-submit').click(function() {
       $('#comment_form').submit();
       alert('Handler for .submit() called + div\'s ID = ' + myDivsId);
       return false;
   });

});​

jsFiddle live demo

jsFiddle现场演示

#2


0  

You need to add a click event for comment-form-submit button.

您需要为comment-form-submit按钮添加click事件。

$(document).on('click','#comment-form-submit', function() {
    $('#comment_form').submit(function() {
       alert('Handler for .submit() called.');
       return false;
    });​
});

#3


0  

You are looking for just submit(). What you are doing in your example is creating a function that will be run when the form is submitted. Also you are not setting up the handler for the clicking to submit the form.

您正在寻找提交()。您在示例中所做的是创建一个在提交表单时运行的函数。此外,您没有设置单击以提交表单的处理程序。

// This creates submit handler for the form
$('#comment_form').submit(function() {
    alert('Handler for .submit() called.');
    return false;
});​

// This creates the on click handler for the submit button
$('#comment-form-submit').on('click', function() {
    // This actually submits the form
    $('#comment_form').submit();
});