通过 .load 传递数组

Pass an array through .load

我有这个 jQuery 代码。它所做的是从(选定的)单选按钮获取值,然后将其传递到 'c' 数组中。

for 语句中有 40 个项目,因此 b<40 条件。我要做的下一步是使用 .load() 将其传递给 PHP。大家可以在下面的代码中看到,我只在c[3]。因为在我想的路上,也许有一种方法可以传递这个数组而不用一个一个地编码。

那么,有没有一种方法可以使用 .load() 将数组传递给 PHP 而无需对它们进行逐个编码,还是我只是懒惰?

$(document).on('click', '#submitexam', function() {
    var examinee = $('#examinee').val();
    var q1 = $('#QU1').attr('id');
    var c = ["null"];
    var b;
    for (b = 1; b < 40; b++) {
        c.push($('input[name=Q' + b + ']:checked').next('label:first').html());
        console.log(c[b]);
    }

    $('.bod').load('process.php', {
        examinee: examinee,
        a1: c[1],
        a2: c[2],
        a3: c[3],
        q1: q1
    })
});

.load()仅用于从服务器获取数据。

您正在使用表单,因此您需要使用 $.post or $.ajax post 将数据发送到服务器。

serialize()函数用于从表单中获取所有数据并将其转换为字符串。

jquery

$(document).on('submit', '#submitexam', function(e) {
    e.preventDefault();
    $.post($(this).attr("action"), $(this).serialize()).done(function(data) {
        console.log(data);
    });
});

html

<form action="process.php" method="post" id="submitexam">
    all your radio buttons place it here and at the end instead of button use
    <input type="submit" value="submit"/>
</form>