dongyong5255 2015-07-27 13:41
浏览 114
已采纳

从列表中动态选择字段并将其添加到表单中

In a form I need to dynamically add (and then remove) some fields (0 to n) from a select.

Every item should be removed from the list after being added so it can't be selected twice.

For example I have a select like this:

 <select name="list">
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
    <option value="4">D</option>
  </select>

And I want to add a new field in my form that shows A and D and stores their values so I can get it through a POST. Then if I click on the select, there will be just B and C.

Can you help me?

  • 写回答

3条回答 默认 最新

  • dsljpwi494719 2015-07-27 13:58
    关注

    What type of fields do you want? Input type text? If so:

    $("[name=list]").on("change", function (e) {
        var val = $(this).val();
        if (val.length > 0) {
            $(this).find("option:selected").remove();
            $("<input>").attr("name", "the-name").val(val).appendTo($("#the-form"))
        }
    });
    

    check out the fiddle https://jsfiddle.net/etoysp7b/1/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)