在javascript中设置序列化数组中的复选框值

时间:2015-11-05 16:40:46

标签: javascript forms serialization

我试图获取一个复选框的值来存储在我的数据库中,但是我的代码在运行序列化数组后立即崩溃。

这是javascript:

$(function () {

$('.form-signin').on('submit', function (e) {

    e.preventDefault();

    var data = $(this).serializeArray(),
        pname = data[0].value,
        score = data[1].value,
        cheatm = data[2].value;

    var GameScore = Parse.Object.extend("GameScore");
    var gs = new GameScore();

    gs.set("score", parseInt(score));
    gs.set("playerName", pname);
    gs.set("cheatMode", cheatm === 'true');
    gs.set("user", Parse.User.current());
.
.
.

在cheatm = data [2] .value;

之后崩溃

这是HTML:

<form class="form-signin" role="form">
                <h2 class="form-signin-heading" id="login-greeting">Enter Game Score</h2>
                <input type="text" name="Player Name" class="form-control" placeholder="Player Name" required="" autofocus="">
                <input type="number" name="Score" class="form-control" placeholder="Score" required="">
                <input type="checkbox" value = 'true'> Cheat Mode<br>
                <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
</form>

1 个答案:

答案 0 :(得分:1)

您可以为复选框指定一个ID,并在JavaScript中调用它,如:

HTML:

<input type="checkbox" id="myCheckbox"/>

jQuery的:

var isMyCheckboxChecked = $("#myCheckbox").is(":checked");