提交没有提交按钮的表单,没有页面刷新?

时间:2022-06-03 20:33:04

I have a form which I cannot change and I have to handle its submit function from jquery only.

我有一个我无法更改的表单,我只能从jquery处理它的提交函数。

The form have radio buttons only. I want to submit the form without the page refereshed when a radio button is checked.

表单只有单选按钮。我想在选中单选按钮时提交没有页面参考的表单。

Here is the code which i tried.

这是我试过的代码。

$('input[type="radio"]').click(function(){
    if ($(this).is(':checked'))
    {
      $('form#my-form').submit(function(){
        alert('form submitted');
      });
    }
});

It should alert the "form submitted" but it does not.

它应警告“提交的表格”,但事实并非如此。

Any thing which I am doing wrong?

我做错了什么?

6 个解决方案

#1


4  

If you want to submit the form without pageload, then you need to go for ajax.

如果你想提交没有pageload的表单,那么你需要去ajax。

If you want the alert while submitting, you need to modify ur code a bit..

如果您在提交时需要提醒,则需要稍微修改一下您的代码..

$('input[type="radio"]').click(function(){
    if ($(this).is(':checked'))
    {
      $('form#my-form').submit();
    }
});

$('form#my-form').submit(function(){
        alert('form submitted');
      });

#2


1  

I tried this example on jsFiddle. My code will submit the form on check of a radioButton.

我在jsFiddle上试过这个例子。我的代码将在检查radioButton时提交表单。

$('input[type="radio"]').click(function(){
    if ($(this).is(":checked"))
      $('form#my-form').submit();
});​

If you'd like to alert anything on submit of your form you have to add this code

如果您想提交表单提醒,则必须添加此代码

$("#my-form").submit(function() {
    alert("submitting form ...");        
});

#3


1  

Your code works well - just make sure you put this code inside document.ready event.

您的代码运行良好 - 只需确保将此代码放在document.ready事件中。

$(document).ready(function() {
    $('input[type="radio"]').click(function(){
        if ($(this).is(':checked'))
        {
          $('form#my-form').submit(function(){
            alert('form submitted');
          });
        }
    });
})

Please note that this selector is looking for ALL radio buttons. Also if you don't need to refresh page - you should add return false; to submit function.

请注意,此选择器正在查找所有单选按钮。此外,如果您不需要刷新页面 - 您应该添加return false;提交功能。

But remember that is your case you need to click on radio button first to make you submit function working.

但请记住,您需要首先单击单选按钮以使您提交功能正常工作。

#4


0  

The code inside your IF says: "If the form is submited then alert user". So you have to submit the form manually when the radio control is checked.

你的IF中的代码说:“如果表单被提交,则提醒用户”。因此,在选中无线电控制时,您必须手动提交表格。

Before closing the IF, enter the following code:

在关闭IF之前,请输入以下代码:

$('yourForm').submit();

#5


0  

In this case you just listen for the submit event. If you want to trigger the submit event you have add this line:

在这种情况下,您只需监听提交事件。如果要触发提交事件,请添加以下行:

$('form#my-form').submit();

#6


0  

Because you want your page to not be refreshed, use Ajax, here is the official documentation:

因为您希望不刷新页面,所以使用Ajax,这是官方文档:

http://api.jquery.com/jQuery.ajax/

#1


4  

If you want to submit the form without pageload, then you need to go for ajax.

如果你想提交没有pageload的表单,那么你需要去ajax。

If you want the alert while submitting, you need to modify ur code a bit..

如果您在提交时需要提醒,则需要稍微修改一下您的代码..

$('input[type="radio"]').click(function(){
    if ($(this).is(':checked'))
    {
      $('form#my-form').submit();
    }
});

$('form#my-form').submit(function(){
        alert('form submitted');
      });

#2


1  

I tried this example on jsFiddle. My code will submit the form on check of a radioButton.

我在jsFiddle上试过这个例子。我的代码将在检查radioButton时提交表单。

$('input[type="radio"]').click(function(){
    if ($(this).is(":checked"))
      $('form#my-form').submit();
});​

If you'd like to alert anything on submit of your form you have to add this code

如果您想提交表单提醒,则必须添加此代码

$("#my-form").submit(function() {
    alert("submitting form ...");        
});

#3


1  

Your code works well - just make sure you put this code inside document.ready event.

您的代码运行良好 - 只需确保将此代码放在document.ready事件中。

$(document).ready(function() {
    $('input[type="radio"]').click(function(){
        if ($(this).is(':checked'))
        {
          $('form#my-form').submit(function(){
            alert('form submitted');
          });
        }
    });
})

Please note that this selector is looking for ALL radio buttons. Also if you don't need to refresh page - you should add return false; to submit function.

请注意,此选择器正在查找所有单选按钮。此外,如果您不需要刷新页面 - 您应该添加return false;提交功能。

But remember that is your case you need to click on radio button first to make you submit function working.

但请记住,您需要首先单击单选按钮以使您提交功能正常工作。

#4


0  

The code inside your IF says: "If the form is submited then alert user". So you have to submit the form manually when the radio control is checked.

你的IF中的代码说:“如果表单被提交,则提醒用户”。因此,在选中无线电控制时,您必须手动提交表格。

Before closing the IF, enter the following code:

在关闭IF之前,请输入以下代码:

$('yourForm').submit();

#5


0  

In this case you just listen for the submit event. If you want to trigger the submit event you have add this line:

在这种情况下,您只需监听提交事件。如果要触发提交事件,请添加以下行:

$('form#my-form').submit();

#6


0  

Because you want your page to not be refreshed, use Ajax, here is the official documentation:

因为您希望不刷新页面,所以使用Ajax,这是官方文档:

http://api.jquery.com/jQuery.ajax/