dongqiang2358 2015-06-27 22:29
浏览 25
已采纳

如何使用js或jquery将选项组合传递给PHP表单

So, for example I have three select boxes.

<select name="cat-1" id="cat-1">
  <option value="0">A</option>
  <option value="1">B</option
</select>
<select name="cat-2" id="cat-2"> 
  <option value="0">AA</option>
  <option value="1">BB</option
</select>
<select name="cat-3" id="cat-3">
  <option value="0">AAA</option>
  <option value="1">BBB</option
</select>
<input type="submit" value="Add">

If I choose options, for example A BB AAA and press ADD, i want this combination to appear as text under add button as: A BB AAA (X) x - to cancel such combination. I would like to add up to 3 combinations with ability to cancel any combination. After submitting form, I want to pass such combinations values in this case I guess array [0, 1, 0] would be fine. Options values cannot be submitted separately, because cat-2 and cat-3 are populated dynamically using ajax - cat-3 depends on cat-2 and cat-2 depends on cat-1 so many combinations are possible.

I am using select2 (http://select2.github.io/examples.html). It would be perfect If I could return combinations in style similar to 'Multiple select boxes' (see link above).

Any idea where do I start? I have no idea how to implement this the right way with ability in future, for example, on click on combination to set select boxes to combination values and etc.

I guess final input to database will be generated array of arrays like [[0,1,0], [0,0,0], [1,0,3], ...] in hidden field?

  • 写回答

1条回答 默认 最新

  • doujia1939 2015-06-28 17:20
    关注

    I have written my own piece of code to handle this problem. Here is working example: http://jsfiddle.net/s2arvdck/13/

    <select name="cat_1" id="cat_1">
        <option value="0">I</option>
        <option value="1">II</option>
        <option value="2">III</option>
    </select>
    <select name="cat_2" id="cat_2">
        <option value="0">A</option>
        <option value="1">B</option>
        <option value="2">C</option>
    </select>
    <select name="cat_3" id="cat_3">
        <option value="0">a</option>
        <option value="1">b</option>
        <option value="2">c</option>
    </select>
    <input type="button" id="addButton" value="Add">
    <br />Combinations: <span id="combContainer"></span>
    
    <br />Expected hidden: <span id="expected"></span>
    
    $('#addButton').on('click', addButtonClicked);
    
    function addButtonClicked(event) {
    
        var cat1 = $('#cat_1 option:selected').val();
        var cat2 = $('#cat_2 option:selected').val();
        var cat3 = $('#cat_3 option:selected').val();
    
        if ($('#combContainer:contains("[' + cat1 + ', ' + cat2 + ', ' + cat3 + ']")').length == 0) {
            $('#combContainer').append('<div><span class="comb">[' + cat1 + ', ' + cat2 + ', ' + cat3 + ']</span> <span id="combDel">remove</span></div>');
        }
    
        var arr = [];
        $('.comb').each(function (index, elem) {
            arr.push($(this).text());
            $('#expected').text(arr.join(''));
        });
    
        $("#combContainer").on("click", "#combDel", function () {
            var text = $(this).closest("div").children(".comb").text();
            if ($('#combContainer:contains("[' + text + ']")').length == 0) {
                $(this).closest("div").remove();
                var expectedText = $("#expected").text();
                var newExpectedText = expectedText.replace(text, '');
                $("#expected").text(newExpectedText);
            }
        });
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器