我如何发送复选框数据转换成JSON阵列? [英] How can I send checkboxes data into JSON array?
本文介绍了我如何发送复选框数据转换成JSON阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的.js文件和我有出口从.js文件复选框回答问题组建成JSON阵列。
I'm new to .js and I have a problem with exporting answers from .js checkboxes form into JSON array.
我的HTML:
<form>
<input type="checkbox" name="Question1" id="Answer1" value="Answer1" onclick="show_checked()"/><label for="Answer1">Answer 1</label><br/>
<input type="checkbox" name="Question1" id="Answer2" value="Answer2" onclick="show_checked()"/><label for="Answer2">Answer 2</label><br/>
<input type="checkbox" name="Question1" id="Answer3" value="Answer3" onclick="show_checked()"/><label for="Answer3">Answer 3</label><br/>
</form>
我的javascript:
my Javascript:
function set_checked(checked)
$('input[name=foo]').attr('checked', checked);
}
$(document).ready(function () {
$("input[name='Question1']").change(function () {
var maxAllowed = 2;
var cnt = $("input[name='Question1']:checked").length;
if (cnt > maxAllowed) {
$(this).prop("checked", "");
alert('Choose max. ' + maxAllowed + ' answers');
}
});
});
现在的问题是,我怎么能发送选中的复选框的ID或值转换成JSON阵列?
The question is, how can I send IDs or values of the checked checkboxes into JSON array?
推荐答案
要获取值到一个数组
var array = $("input[name='Question1']:checked").map(function(){
return this.value;
}).get()
让IDS到一个数组
to get ids to an array
var array = $("input[name='Question1']:checked").map(function(){
return this.id;
}).get()
这篇关于我如何发送复选框数据转换成JSON阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文