1

首先,我刚开始使用 jQuery,所以请多多包涵!

我正在尝试将我的数据转换为以下格式...

{
"qualifications": [
    "1",
    "7"
],
units: [
    "7",
    "3",
    "1"
]
}

HTML是....

        <div class="control-group ">
    <label for="qualifications" class="control-label required">Qualifications</label>
    <div id="qual-holder" class="controls">
        <table class="table">
            <tbody>
                <tr>
                    <td>Induction</td>
                    <td>
                        <div class="switch" data-on-label="In" data-on="info" data-off-label="Out">
                            <input id="qual" name="qual[1]" type="checkbox" value="1">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>LearnerZone Academy</td>
                    <td>
                        <div class="switch" data-on-label="In" data-on="info" data-off-label="Out">
                            <input id="qual" name="qual[7]" type="checkbox" value="7">
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<div class="control-group ">
    <label for="units" class="control-label required">Units</label>
    <div id="unit-holder" class="controls">
        <table class="table">
            <tbody>
                <tr>
                    <td>Data Protection (DP1)</td>
                    <td>
                        <div class="switch" data-on-label="In" data-on="info" data-off-label="Out">
                            <input id="unit" name="unit[7]" type="checkbox" value="7">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Equal Opportunities (EQ1)</td>
                    <td>
                        <div class="switch" data-on-label="In" data-on="info" data-off-label="Out">
                            <input id="unit" name="unit[3]" type="checkbox" value="3">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Health &amp; Safety (HS1)</td>
                    <td>
                        <div class="switch" data-on-label="In" data-on="info" data-off-label="Out">
                            <input id="unit" name="unit[1]" type="checkbox" value="1">
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

最后是我的非工作 jQuery ......

function questionCount() {
// Create the arrays
var qualifications = [];
var units = [];

// Search the DOM for checked quals
$.each('input[name="qual[]"]:checked', function() {
    // This creates an object
    var value = $(this).val();
    qualifications.push(value);
});

// Search the DOM for checked units
$.each('input[name="unit[]"]:checked', function() {
    // This creates an object
    var value = $(this).val();
    units.push(value);
});

alert("Qualifications = " + JSON.stringify(qualifications) + " Units = " + JSON.stringify(units));
}

$("input[type=checkbox]").on( 'change', questionCount() );

这个想法是用户将检查一个或多个复选框,然后我可以将其传递给 ajax 调用以缩小选择计数。

有人能帮助我吗?

4

1 回答 1

0

jsFiddle

从复选框名称中删除值,使它们变为:

<input id="qual" name="qual[]" type="checkbox" value="1">
                           ^ nothing in here
<input id="unit" name="unit[]" type="checkbox" value="7">

.each也错了。您将一个字符串作为第一个参数传递,但您需要使用 jQuery 包装选择器$(),更改为:

$.each($('input[name="qual[]"]:checked'), function() {

$.each($('input[name="unit[]"]:checked'), function() {

然后对于您的最终 JSON:

var json = JSON.stringify({ qualifications : qualifications, units : units });

输出(全部选中):

{
    "qualifications": [
        "1",
        "7"
    ],
    "units": [
        "7",
        "3",
        "1"
    ]
}
于 2013-07-10T16:08:54.317 回答