【问题标题】:jQuery to alter the MYSQL tables column name dynamicallyjQuery 动态更改 MYSQL 表的列名
【发布时间】:2015-06-22 14:39:54
【问题描述】:

我的页面中有一个表格,我正在使用 mysql。问题是我想动态地添加一个新字段。该选项添加列名,然后添加值。添加列字段后,它应该要求为输入数据添加列。 我正在考虑在从用户那里获取列名后更改列名。 是否可以。 这可能是解决这个问题的有效方法。

为了清楚起见......问题是

我有 3 列(在我的 MYSQL 表中总共 6 列)说 COL1、COL2、COL3。 当我要求输入时,它是这样的:

 COL1:  ___________________
 COL2:  ___________________
 COL3:  ___________________
 Do you want more fields : y/N

如果是,那么我想为它们添加 COL4、COL5、COL6,最大值为 3。 (我在表中添加了 6 列,其中 3 列具有 NULL 值以供额外使用) 另外,我必须允许用户决定列名。更清楚地说,如果选择“y”,则会弹出一个包含两个字段的弹出窗口 - 一个用于列名称及其各自的数据

_________________   :  ________________

。之后,列名应该能够直接显示并且只要求数据。

    COL1:  ___________________
    COL2:  ___________________
    COL3:  ___________________
   theCOLUMNNAME  : ___________________
   Do you want more fields : y/N

编辑

我已经使用 jQuery 像这样动态添加字段条目:

$(document).on('click', '.add_button', function(){
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var fieldHTML = '<div><input type="text" name="field_name[]" value=""/>  <input type="text" name="field_name[]" value=""/><a href="javascript:void(0);" class="remove_button" title="Remove field"><img src="images/remove-icon.png"/></a></div>'; //New input field html 
    if(x < maxField){ //Check maximum number of input fields
        x++; //Increment field counter
        $(wrapper).append(fieldHTML); // Add field html
    }
    $(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
    e.preventDefault();
    $(this).parent('div').remove(); //Remove field html
    x--; //Decrement field counter
});

 });

【问题讨论】:

    标签: php jquery mysql drupal-7


    【解决方案1】:

    如果您想为输入设置一个新名称或 ID,您必须使用 提示

    var newid = prompt("Set the new ID"); 
    

    允许您输入一个值并在 var 中分配该值,然后在输入中设置。

    好的,让我们开始吧!

    首先:替换id:

    var id = "input" + $(this).attr("id").replace("field","");
    

    第二:询问新的ID、名称或值

    id = prompt("Set the new ID");
    

    第三:为标签分配id和值

    var label = $("<label for=\"" + id + "\">" + id + "</label>");
    

    第四:为输入分配id、名称或值

    input = $("<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\"+ value=\"" + id + "\" />");
    

    这里是代码。我用了这个例子:https://jsfiddle.net/qBURS/2/ 我修改了 JS

    HTML

    <html>
    <head></head>
    <body>
    <fieldset id="buildyourform">
        <legend>Build your own form!</legend>
    </fieldset>
    <input type="button" value="Preview form" class="add" id="preview" />
    <input type="button" value="Add a field" class="add" id="add" />
    </body>
    </html>
    

    JS

    <script>
    
    $(document).ready(function() {
      $("#add").click(function() {
        var intId = $("#buildyourform div").length + 1;
        var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
        var fName = $("<input type=\"text\" class=\"fieldname\" />");
        var fType = $("<select class=\"fieldtype\"><option value=\"checkbox\">Checked</option><option value=\"textbox\">Text</option><option value=\"textarea\">Paragraph</option></select>");
        var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");
        removeButton.click(function() {
            $(this).parent().remove();
        });
        fieldWrapper.append(fName);
        fieldWrapper.append(fType);
        fieldWrapper.append(removeButton);
        $("#buildyourform").append(fieldWrapper);
    });
    $("#preview").click(function() {
        $("#yourform").remove();
        var fieldSet = $("<fieldset id=\"yourform\"><legend>Your Form</legend></fieldset>");
        $("#buildyourform div").each(function() {
            var id = "input" + $(this).attr("id").replace("field","");
    
            id = prompt("Set the new ID");
    
            var label = $("<label for=\"" + id + "\">" + id + "</label>");
            var input;
            switch ($(this).find("select.fieldtype").first().val()) {
                case "checkbox":
                    input = $("<input type=\"checkbox\" id=\"" + id + "\" name=\"" + id + "\" />");
                    break;
                case "textbox":
                    input = $("<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\"+ value=\"" + id + "\" />");
                    break;
                case "textarea":
                    input = $("<textarea id=\"" + id + "\" name=\"" + id + "\" ></textarea>");
                    break;    
            }
            fieldSet.append(label);
            fieldSet.append(input);
    
        });
        $("body").append(fieldSet);
      });
    });
    
    </script>
    

    【讨论】:

    • 你能告诉我们如何在 MYSQL 表中动态完成。就像动态地将新字段添加到 MYSQL 表中一样。
    • 不客气;如果你需要 mysql 你可以使用插入和删除,我不知道我是否理解你的问题
    猜你喜欢
    • 1970-01-01
    • 2019-08-30
    • 2016-06-19
    • 1970-01-01
    • 2017-01-31
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多