jQuery 单击单选按钮提交表单 - 提交表单并通过单选按钮执行其他操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13126501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:18:54  来源:igfitidea点击:

Submit form on radio button click - Submit form and perform other actions on radio click

jquery

提问by Michael Fever

I have a jQuery question concerning Radio buttons and forms.

我有一个关于单选按钮和表单的 jQuery 问题。

I have tabbed content setup with 5 tabs setup with a form in each tab.

我有 5 个选项卡设置的选项卡式内容设置,每个选项卡中都有一个表单。

Each form consists of 5 radio buttons and a submit button.

每个表单包含 5 个单选按钮和一个提交按钮。

I have my jQuery setup to validatte that a selection has been made first, then submit the form using AJAX, close the current tab (slideUp) and open the next tab (slideDown).

我有我的 jQuery 设置来验证已首先进行选择,然后使用 AJAX 提交表单,关闭当前选项卡 (slideUp) 并打开下一个选项卡 (slideDown)。

What I would prefer to do is remove the submit buttons and submit the form on the click of any of the radiobuttons. So that clicking a radio button submits the form, closes the current tab and opens the next.

我更愿意做的是删除提交按钮并在单击任何单选按钮时提交表单。因此,单击单选按钮会提交表单,关闭当前选项卡并打开下一个选项卡。

What do I need to do to make it so that clicking any one of the radio buttons will submit the forma and close the tabs. My jQuery code is posted below.

我需要做什么才能使单击任一单选按钮提交表单并关闭选项卡。我的 jQuery 代码发布在下面。

$("#selfAss1").validate({
    invalidHandler: function(e, validator) {
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1
                ? 'You missed 1 field. It has been highlighted below'
                : 'You missed ' + errors + ' fields.  They have been highlighted below';
            $("div.error span").html(message);
            $("div.error").show();
        } else {
            $("div.error").hide();
        }
    },
    onkeyup: false,
    submitHandler: function() {
            $.post('/_action.php', $("#selfAss1").serialize(), function(data) {
                    $('.pane1').slideUp('slow');
                    $('#result1').html(data);
                    $('.pane2').slideDown('slow');


            });
    },
    debug:true
});

回答by Rory McCrossan

The transition from one slide to the next is handled by your submitHandler, so all the radio buttons need to do is submit the form on click, with that in mind:

从一张幻灯片到下一张幻灯片的转换由您的 处理submitHandler,因此所有单选按钮需要做的就是在单击时提交表单,记住这一点:

$('input[type=radio]').on('change', function() {
    $(this).closest("form").submit();
});

A couple of notes; firstly I used the attribute selector here, you may want to change that to a class or something more specific for your needs. Secondly, you may also want to include a 'return' method to allow people to change their selections. With this method, if you click once, that's it.

一些注意事项;首先,我在这里使用了属性选择器,您可能希望将其更改为类或更具体的内容以满足您的需求。其次,您可能还希望包含一个“返回”方法以允许人们更改他们的选择。使用这种方法,如果您单击一次,就是这样。

回答by NullPoiиteя

try

尝试

 <input type="radio" name="something" onclick="document.getElementById('myForm').submit();"/> 

or

或者

 <input type="radio" name="something" onclick="this.form.submit()">

in jquery try something like

在 jquery 中尝试类似

$('input[type=radio]').click(function() {
    $("form id or class").submit();
});

回答by Graeme Mehl

This worked for me:

这对我有用:

<input name="myRadio" type="radio" value="1" onchange="this.form.submit()"/>

回答by Toki

Try This!!

尝试这个!!

 $('input[type=radio]').on('change', function() {
$('input[type=submit]').click();});