首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JQuery-Jtable中动态添加字段

在JQuery-Jtable中动态添加字段
EN

Stack Overflow用户
提问于 2013-01-29 13:43:05
回答 2查看 2.2K关注 0票数 1

如何在Jtable中动态添加字段。我想要多个城市的值,请参考附件中的图片

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-16 08:53:19

是的,这不是jQuery jTable内置的。为了解决这个问题,我为同样的目的创建了一个脚本。这处理(a)添加更多控件或控件组以及(b)移除控件。

下面是脚本:

代码语言:javascript
复制
//add '.add_more' class to
$(".add_more").on('click', function () {

    // creates unique id for each element
    var _uniqueid_ = +Math.floor(Math.random() * 1000000);
    var new_ele_id = $(this).attr("data-clone-target") + _uniqueid_;

    var cloneObj = $("#" + $(this).attr("data-clone-target"))
    .clone()
    .val('')
    .attr("id", new_ele_id);

    // if the control is grouped control
    if ($(this).hasClass('group_control') == true) {
        $($(cloneObj).children()).each(function () {
            $(this).attr("id", $(this).attr("id") + _uniqueid_).val("");
        });
    }

    $(cloneObj).insertBefore($(this));

    //creates a 'remove' link for each created element or grouped element
    $("<a href='javascript:void(0);' class='remove_this' data-target-id='" + new_ele_id + "'></a>")
    .on('click', function () {
        if ($(this).is(":visible") == true) {
            if (confirm("Are you sure?")) {
                $("#" + $(this).attr("data-target-id")).remove();
                $(this).remove();
            }
        }
        else {
            $("#" + $(this).attr("data-target-id")).remove();
            $(this).remove();
        }

    }).insertBefore($(this));
    $("#" + new_ele_id).focus();



});

//remove element script
$(".remove_this").on('click', function () {
    if ($(this).is(":visible") == true) {
        if (confirm("Are you sure?")) {
            $("#" + $(this).attr("data-target-id")).remove();
            $(this).remove();
        }
    }
    else {
        $("#" + $(this).attr("data-target-id")).remove();
        $(this).remove();
    }
});

用法: Single Element http://jsfiddle.net/vkscorpion1986/ktbn4qLg/2/

代码语言:javascript
复制
<input class="" id="<ELEMENT-ID>" type="text" name="input1">
<a href="javascript:void(0);" data-clone-target="<ELEMENT-ID>" title="Add More" class="add_more">Add More</a>

用法: Grouped http://jsfiddle.net/vkscorpion1986/ktbn4qLg/4/

代码语言:javascript
复制
<div id="<ELEMENT-ID>">
    <input class="" id="input1" type="text" name="input1">
    <input class="" id="input2" type="text" name="input2">
</div>
<a href="javascript:void(0);" data-clone-target="<ELEMENT-ID>" title="Add More" class="add_more group_control">Add More</a>

属性

代码语言:javascript
复制
href = javascript:void(0); // just to disable the anchor tag default behaviour 
data-clone-target = id of the target element

css类

代码语言:javascript
复制
.add_more = to implement the add more/remove controls functionality
.group_control  = for indicating that this is group of elements which have to be repeted

希望这对你有用。

票数 1
EN

Stack Overflow用户

发布于 2013-01-29 22:48:19

不,它不是用jTable做的。您可以使用输入选项(http://jtable.org/ApiReference#fopt-input)和下面的命令:http://jqueryui.com/autocomplete/#multiple,也可以创建自己的对话框。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14576385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档