【问题标题】:ajaxform select2 concatenate multiple IDsajaxform select2连接多个ID
【发布时间】:2016-06-04 07:51:16
【问题描述】:

我有一个带有 ajaxform 的 select2 搜索表单,它将新表单条目连接到原始 select2 搜索表单中。如果添加了多个新条目,则新文本值会正确连接到搜索字段中,但是,任何新的隐藏 ID 都会替换现有 ID。它似乎已添加,因为所有新文本值都显示在 select2 搜索字段中。我认为问题在于,除了与搜索字段连接的文本字段外,新 ID 还应连接到隐藏的cropstageid 字段。我不知道该怎么做。感谢您的帮助。

<script>
$(document).ready(function() { 

$("#search").select2({
width: '60%',
allowClear: true,
minimumInputLength: 0
});

$('#btnSubmit').on('click', function() {

$.ajax({
    asynch : true,
    type : 'POST',
    dataType : 'json',
    url: '../cfc/stages.cfc?method=addstage&returnformat=json',
    //all form fields submitted to url
    data: $("form").serialize(),

    success : function(data, textStatus) {
        //close the modal
        $('#stagemodal').modal('hide');

        //set the returned data to a variable
        var fullname = $('#stagename').val();
        $("#cropstageid").val(data.DATA);

        //get current selection
        var selection = $(search).select2("data");
        //add a new item to the selection
        selection.push({id: data.DATA, text: fullname})
        //set the updated selection
        $(search).select2("data",selection);

        //reset form
        $('#addstageform')[0].reset();
        //output data to console
        console.log(data.DATA);

}
});
});

});
</script>


<cfform name="stageform" id="stageform" action="settings_form_action.cfm" method="post" ENCTYPE="multipart/form-data">

<h4 class="text-primary">Select Crop Stages</h4>
<input type="hidden" id="cropstageid" name="cropstageid">

<cfselect id="search" multiple name="cropstageid" >
<cfloop query="stages" >
<option value="#cropstageid#" >#stage#</option>
</cfloop>

</cfform>

*AjaxForm 用于新条目

<cfform id="addstageform" name="addstageform" action="" method="post">
<input type="text" name="stagename" id="stagename" autofocus size="35">
<input type="button" id="btnSubmit" value="Add" /><
</cfform>

【问题讨论】:

    标签: concatenation ajaxform select2


    【解决方案1】:

    感谢同事的帮助,解决方法如下:

    1. 成功后,我们不再附加到隐藏字段,因此删除 $("#cropstageid").val(data.DATA);
    2. 成功后,添加 $('#search').append('' + 全名 + ''); 这一行从新添加的 ajaxform 记录中添加了另一个选择选项
    3. 不再需要隐藏值,因为它作为选择选项附加,因此删除表单内隐藏的cropstageid 表单字段。

    清理后的脚本如下:

    <script>
    $(document).ready(function() { 
    
    $("#search").select2({
    width: '60%',
    allowClear: true,
    minimumInputLength: 0
    });
    
    $('#btnSubmit').on('click', function() {
    
    $.ajax({
    asynch : true,
    type : 'POST',
    dataType : 'json',
    url: '../cfc/stages.cfc?method=addstage&returnformat=json',
    //all form fields submitted to url
    data: $("form").serialize(),
    
    success : function(data, textStatus) {
    //close the modal
    $('#stagemodal').modal('hide');
    //set the returned data to a variable
    var fullname = $('#stagename').val();
    //get current selection
    var selection = $('#search').select2("data");
    //add the new option to the select 
    $('#search').append('<option value="' + data.DATA + '">' + fullname +    '</option>');
    //add a new item to the selection array
    selection.push({
    id: data.DATA, 
    text: fullname
    });
    //set the updated selection
    $('#search').select2("data",selection);
    //reset the modal form
    $('#addstageform')[0].reset();
    //output to the console
    console.log(data.DATA);
    }
    
    });
    });
    
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多