这个代码出了什么问题?错误丢失”)&”;

What is wrong with this code? Error missing ")"

本文关键字:错误 代码 问题 什么      更新时间:2023-09-26

除了这行之外,代码仍在正常工作

$('#ReplenishItem').append('<tr><td><input type="hidden" name="" value="' + data[0].itemCode + '"/>'n'<input type="text" value="' + data[0].productName + '"/> 'n'</td><td>'n'<select name="size">'
for (var y in data) {
    ''<option value="' + data[y].size + '">' + data[y].size + ''</option>'
}
''</select>'n'</td>'n'</tr>');
}

我试着删除上面的一行,它有效,也试着在没有循环的情况下追加,它有效。我不需要正确的方法来做这件事,但希望上面的代码可以工作

var x = true;
function autoCompleteWarehouseInventory() {
    $("#productName").devbridgeAutocomplete({
        serviceUrl: 'searchWarehouseInv',
        type: 'POST',
        showNoSuggestionNotice: true,
        noSuggestionNotice: 'No Exsiting Product',
        onSelect: function (event, ui) {
            var productName = document.getElementById('productName').value;
            $.ajax({
                type: 'POST',
                url: 'SetWarehouseInvServlet',
                dataType: 'json',
                data: {
                    productName: productName
                },
                success: function (data) {
                    if (x) {
                        $('#ReplenishItem').append('<tr>'n'
                            <th>Product Name</th><th>Color</th><th>Size</th><th>Quantity</th></tr>');
                        x = false;
                    };
                    $('#ReplenishItem').append('<tr><td><input type="hidden" name="" value="' + data[0].itemCode + '"/>'n'<input type="text" value="' + data[0].productName + '"/> 'n'</td><td>'n'<select name="size">'
                    for (var y in data) {
                        ''<option value="' + data[y].size + '">' + data[y].size + ''</option>'
                    }
                    ''</select>'n'</td>'n'</tr>')
                }
            });
        }
    });
}

不能让for...in循环与这样的字符串串联在一起。

可以使用Array.prototype.mapArray.prototype.join来格式化data中的<option>元素字符串。

例如:

$('#ReplenishItem').append('<tr><td><input type="hidden" name="" value="'+ data[0].itemCode +'"/>'n'
                           <input type="text" value="'+ data[0].productName +'"/> 'n'
                           <td><td>'n'
                            <select name="size">' 
                               + data.map(function(opt) { return '<option value="'+ opt.size +'">'+ opt.size + '</option>' }).join("") 
                           + '</select>'n'
                           </td>'n'
                           </tr>');

您应该尝试使用一个将大括号和圆括号配对的编辑器。:)

试试这个:

var x =true;
function autoCompleteWarehouseInventory() {
    $("#productName").devbridgeAutocomplete({
        serviceUrl: 'searchWarehouseInv',
        type: 'POST',
        showNoSuggestionNotice: true,
        noSuggestionNotice: 'No Exsiting Product',
        onSelect: function (event, ui) {
            var productName = document.getElementById('productName').value;
            $.ajax({
                type: 'POST',
                url: 'SetWarehouseInvServlet',
                dataType: 'json',
                data: {
                    productName: productName
                },
                success: function (data) {
                    if(x){
                        $('#ReplenishItem').append('<tr>'n<th>Product Name</th><th>Color</th><th>Size</th><th>Quantity</th></tr>');
                        x = false;
                    }
                    var options = '';
                    for(var y in data){
                        options += '<option value="'+ data[y].size +'">'+ data[y].size + '</option>';
                    }
                    $('#ReplenishItem').append(
                            '<tr>'+
                                '<td>'+
                                    '<input type="hidden" name="" value="'+ data[0].itemCode +'"/>'+
                                    '<input type="text" value="'+ data[0].productName +'"/>'+
                                '</td>'+
                                '<td>'+
                                    '<select name="size">'+
                                        options
                                    '</select>'+
                                '</td>'+
                            '</tr>');
                }
            });
        }
    });
}