在表单提交上获取复选框值,并使用jQuery存储在变量中 [英] Get checkbox value on form submit and store in a variable using jQuery

查看:85
本文介绍了在表单提交上获取复选框值,并使用jQuery存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个表格使用多个复选框与一个类'test'(只有一个可以检查,但类似于使用单选按钮 - 不是我的想法btw!)。



当表单提交时,我想获取被选中的复选框的值并将其存储在变量中 - 任何人都可以在这里帮助?



干杯!

pre> (form)。submit(function(){
var aVariable = $('input.test [type =checkbox]:checked',this).val();
});

.val() $ c> undefined 如果调用一个空的jQuery对象,这将是这里的情况下,如果没有复选框被选中。


Have a form using multiple checkboxes with a class of 'test' (only one of which can be checked however similar to using radio buttons - not my idea btw!).

When the form is submitted I want to get the value of the checkbox that is checked and store it in a variable - can anyone help here?

Cheers!

Chris

解决方案

$("form").submit(function() {
    var aVariable = $('input.test[type="checkbox"]:checked', this).val();
});

.val() returns undefined if called on an empty jQuery object, which would be the case here if no checkboxes are checked.

这篇关于在表单提交上获取复选框值,并使用jQuery存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆