Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过JS函数添加行,选择选项中有一个单元格_Javascript_Html_Bootstrap 4 - Fatal编程技术网

Javascript 通过JS函数添加行,选择选项中有一个单元格

Javascript 通过JS函数添加行,选择选项中有一个单元格,javascript,html,bootstrap-4,Javascript,Html,Bootstrap 4,我试图通过点击一个按钮将行添加到我的引导表中,该按钮按预期工作。新行中的每个单元格都包含文本框。现在我想要一个带有选项的选择框的单元格 为了动态添加行,我使用以下js函数: $(document).ready(function(){ $('[data-toggle="tooltip"]').tooltip(); var actions = $("table td:last-child").html(); // Append table with add row form

我试图通过点击一个按钮将行添加到我的引导表中,该按钮按预期工作。新行中的每个单元格都包含文本框。现在我想要一个带有选项的选择框的单元格

为了动态添加行,我使用以下js函数:

$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();
    var actions = $("table td:last-child").html();
    // Append table with add row form on add new button click
    $(".add-new").click(function(){
        $(this).attr("disabled", "disabled");
        var index = $("table tbody tr:last-child").index();
        var row = '<tr>' +
            '<td><input type="text" class="form-control" name="name" id="name"></td>' +
            '<td><input type="text" class="form-control" name="department" id="department"></td>' +
            '<td><input type="text" class="form-control" name="phone" id="phone"></td>' +
            '<td>' + actions + '</td>' +
        '</tr>';
        $("table").append(row);     
        $("table tbody tr").eq(index + 1).find(".add, .edit").toggle();
        $('[data-toggle="tooltip"]').tooltip();
    });
    // Add row on add button click
    $(document).on("click", ".add", function(){
        var empty = false;
        var input = $(this).parents("tr").find('input[type="text"]');
        input.each(function(){
            if(!$(this).val()){
                $(this).addClass("error");
                empty = true;
            } else{
                $(this).removeClass("error");
            }
        });
        $(this).parents("tr").find(".error").first().focus();
        if(!empty){
            input.each(function(){
                $(this).parent("td").html($(this).val());
            });         
            $(this).parents("tr").find(".add, .edit").toggle();
            $(".add-new").removeAttr("disabled");
        }       
    });
    // Edit row on edit button click
    $(document).on("click", ".edit", function(){        
        $(this).parents("tr").find("td:not(:last-child)").each(function(){
            $(this).html('<input type="text" class="form-control" value="' + $(this).text() + '">');
        });     
        $(this).parents("tr").find(".add, .edit").toggle();
        $(".add-new").attr("disabled", "disabled");
    });
    // Delete row on delete button click
    $(document).on("click", ".delete", function(){
        $(this).parents("tr").remove();
        $(".add-new").removeAttr("disabled");
    });
});

$(文档).ready(函数(){
$('[data toggle=“tooltip”]')。tooltip();
var actions=$(“表td:last child”).html();
//在“添加新”按钮上使用“添加行”窗体追加表单击
$(“.add new”)。单击(函数(){
$(此).attr(“已禁用”、“已禁用”);
var index=$(“表tbody tr:last child”).index();
变量行=“”+
'' +
'' +
'' +
''+行动+''中+
'';
$(“表格”)。追加(第行);
$(“table tbody tr”).eq(索引+1).find(“.add,.edit”).toggle();
$('[data toggle=“tooltip”]')。tooltip();
});
//单击“添加”按钮上的“添加行”
$(文档)。在(“单击“,”.add“,函数()上){
var empty=false;
var input=$(this.parents(“tr”).find('input[type=“text”]”);
input.each(函数(){
if(!$(this.val()){
$(this.addClass(“错误”);
空=真;
}否则{
$(此).removeClass(“错误”);
}
});
$(this.parents(“tr”).find(“.error”).first().focus();
如果(!空){
input.each(函数(){
$(this.parent(“td”).html($(this.val());
});         
$(this.parents(“tr”).find(“.add.edit”).toggle();
$(“.add new”).removeAttr(“禁用”);
}       
});
//单击“编辑”按钮上的“编辑行”
$(文档)。在(“单击”、“.edit”上,函数(){
$(this).parents(“tr”).find(“td:not(:last child)”).each(function(){
$(this.html(“”);
});     
$(this.parents(“tr”).find(“.add.edit”).toggle();
$(“.add new”).attr(“已禁用”、“已禁用”);
});
//单击“删除”按钮删除行
$(文档)。在(“单击“,”.delete“,函数()上){
$(this.parents(“tr”).remove();
$(“.add new”).removeAttr(“禁用”);
});
});
现在,我希望在部门中允许用户选择: 所以我替换了:

'<td><input type="text" class="form-control" name="department" id="department"></td>'
“”
以下是:

'<td><select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select></td>'
'
沃尔沃汽车
萨博
梅赛德斯
奥迪
'

但它不能正常工作。有没有关于选择文本以外的其他表单字段的建议?

您需要关闭字符串(单引号),这里的示例代码希望您能理解这一点

也许你可以试试把手或者小胡子,这样会让你的代码更容易阅读

    var row = '<tr>';
        row += '<td><input type="text" class="form-control" name="name" id="name"></td>';
        row += '<td>';
        row += '<select>';
        row += '<option value="volvo"> Volvo</option>';
        row += '<option value="saab">Saab</option>';
        row += '<option value="mercedes">Mercedes</option>';
        row += '<option value="audi">Audi</option>';
        row += '</select>';
        row += '</td>';
        row += '<td><input type="text" class="form-control" name="phone" id="phone"></td>';
        row += '<td>' + actions + '</td>';
        row += '</tr>';
var行=”;
行+='';
行+='';
行+='';
行+=‘沃尔沃’;
row+=‘萨博’;
row+=‘梅赛德斯’;
世界其他地区+=‘奥迪’;
行+='';
行+='';
行+='';
行+=''+操作+'';
行+='';