繁体   English   中英

jQuery动态更改MYSQL表的列名

[英]jQuery to alter the MYSQL tables column name dynamically

我的页面中有一张表,并且使用mysql进行访问。 问题是我想向其动态添加一个新字段。 该选项添加列名,然后添加值。 添加列字段之后,它应该要求添加的列作为输入数据。 我想在从用户那里获得列名后更改列名。 可能吗。 这可能是解决此问题的有效方法。

为了清楚...问题是

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

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

如果是,那么我想为它们添加COL4,COL5,COL6的最大值为3 (我在表中添加了6列,其中3列带有NULL值,以供额外使用)此外,我还必须允许用户确定COLUMN名称。 更明确的方式是,如果选择“ 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
});

 });

如果要为输入设置新名称或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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM