如何将带有optgroup的<select>转换为JSON数组</select>

时间:2015-04-21 13:10:40

标签: javascript php jquery json

HTML:

<select multiple size="10" class="show" id="boxA"></select>
<nav id="buttons">
    <ul class="small-block-grid-2 medium-block-grid-2 large-block-grid-2">
        <li><a class="remove" href="#">Remove</a></li>
        <li><a class="add" href="#">Add</a></li>
    </ul>
</nav>
<select multiple size="10" class="show" id="boxB"> </select>

如何将<select multiple size="10" class="show" id="boxB"> </select>转换为JSON数组,以便我可以通过ajax将其发送到服务器?

为您的信息选择框,其中包含由<optgroup>.

的PHP文件创建的ID boxB

PHP文件:

$last_category = "";
while ($row = mysqli_fetch_array($result)) {
    $levels = $row['level_name'];
    $levels_id = $row['level_id'];
    $subjects = $row['subject_name'];
    $subjects_id = $row['subject_id'];
    if ($last_category != $levels) {
        $last_category = $levels;
        echo '<optgroup label="' . $levels . '" class="' . $q .'">';
    }
    echo '<option value="' . $subjects_id . '">' . $subjects . '</option>';
    if ($last_category != $levels)
        echo '</optgroup>';
}

1 个答案:

答案 0 :(得分:0)

您可以这样做:

var json = $(':input').serializeArray()
var string = JSON.stringify(json)