从多个选择提交的Javascript

时间:2017-08-04 21:04:25

标签: javascript php jquery html css

我有这段代码:

<div class="st-form-line">
        <span class="st-labeltext">Countries</span>
        <label class="margin-right10"><input type="radio" id="members_create_campaign_form_countrySelectionType_0" name="members_create_campaign_form[countrySelectionType]" required="required" value="0" checked="checked" /> All</label>
        <label class="margin-right10"><input type="radio" id="members_create_campaign_form_countrySelectionType_1" name="members_create_campaign_form[countrySelectionType]" required="required" value="1"/> Selected</label>
        <div id="clist_div" class="simplebox cgrid540-right" style="display:none;">
            <div style="padding:5px"></div>
            <div class="simplebox cgrid200-left">
                <p style="text-align:center;"><b>Excluded Countries</b></p>
                <select size="10" name="excluded2" style="width:200px; height:160px;" onDblClick="moveSelectedOptions(this.form['excluded2'],this.form['countries[]'])" multiple >
                                                                               <?php foreach($arrayCountries as $country) {?>
                                                <option value="<?= $country ?>" ><?= $country ?></option>
                                            <?php } ?>

              </select>
            </div>
            <div class="simplebox cgrid40-left">
                <input class="button-blue" type="button" name="right" value="&gt;&gt;" onclick="moveSelectedOptions(this.form['excluded2'],this.form['countries[]'])"><br/><br/>
                <input class="button-blue" type="button" name="left" value="&lt;&lt;" onclick="moveSelectedOptions(this.form['countries[]'],this.form['excluded2'])">
            </div>
            <div class="simplebox cgrid200-left">
                <p style="text-align:center;"><b>Selected Countries</b></p>
                <select size="10" id="members_create_campaign_form_countries" name="countries[]" style="width:200px; height:160px;" onDblClick="moveSelectedOptions(this.form['countries[]'],this.form['excluded2'])" multiple >
                                        </select>
            </div>

        </div>
        <div class="clear"></div>

    </div>  

此代码如下所示: enter image description here

我从左侧选择一些国家后将我添加到右侧,好的,这很好,但我的问题是如果未选中此照片 enter image description here

没有在我的数据库中添加,并且在这个screnshoot中它只添加了加入和德国的选择,通常我想添加在右侧添加的所有国家。

这是js代码:

<script type="text/javascript">
$(document).ready(function () {
    if ($('#members_create_campaign_form_countrySelectionType_1').is(':checked')) {
        $('#clist_div').show('slow');
    }
    $('#members_create_campaign_form_countrySelectionType_0').click(function () {
        $('#clist_div').hide('slow');
    });
    $('#members_create_campaign_form_countrySelectionType_1').click(function () {
        $('#clist_div').show('slow');
    });

    function selectDiff(s1Id, s2Id) {
        var selected = $('#' + s2Id + ' option').map(function(){
            return this.value;
        }).get()

        var excluded = $('#' + s1Id + ' option').each(function() {
            if (selected.indexOf(this.value) != -1) {
                selected.splice(selected.indexOf(this.value), 1);
                $(this).remove();
            }
        }).get()
    };

    selectDiff('clist_div', 'members_create_campaign_form_countries');
});

2 个答案:

答案 0 :(得分:0)

第一次select the all selected countries

需要on submit选项

第二名:在selected attribute的{​​{1}}中简单添加all option

注意:因此,如果您添加到selected countries并从selected countries删除则意味着。选项selected countries loose the selected,因此您需要在提交

上添加attribute
selected attribute

答案 1 :(得分:0)

您可以将所选国家/地区作为数组,并继续推送选定的国家/地区。

例如:

var selected_countries = [];

//在选择国家/地区

selected_countries.push('country selected');

使用selected_countries数组添加到DB