我想阻止用户在input[type='text']
为空时输入空格键。如果不是在输入空格键后,应抓取input[type='text']
的值并将其放在span
标记中。现在我想指定for-loop
来执行第二部分(我的意思是value
只放入span
)五次。当 5 span
已经存在时,不再这样做了。我应该在哪里添加for-loops
?
这是我的代码:
$(function()
{
$("#tags-selected").on('keypress', function(e)
{
var tags_selected=$("#tags-selected").val();
if(e.which === 32)
{
if(!this.value.length)
e.preventDefault();
else
$("<span class='suggested-tag'>"+tags_selected+"<span class='closee'>XX</span></span>").insertBefore("#tags-selected");
$("#tags-selected").val('');
$(".tags-review").fadeOut(300);
}
});
});
答案 0 :(得分:5)
您无需为此添加循环。只需使用类suggest-tag检查附加span元素的长度。并且只有在长度小于5时才附加新元素:
string pitpivotNam1 = "table1";
Dictionary<String, Excel.PivotField> pFields = new Dictionary<String, Excel.PivotField>();
Excel.PivotTable pivotTable1 = null;
Excel.Range pivotDest = excelApp.Worksheets["PivotTable"].Cells[1, 1];
excelApp.Worksheets[sheetData].Activate();
int countColmn = excelApp.Cells[1, 1].End(Excel.XlDirection.xlToRight).Column;
var headerNam = excelApp.Range[excelApp.Cells[1, 1], excelApp.Cells[1, countColmn]].Value;
// Data Source for PivotTable
Excel.Range pivotData = excelApp.Worksheets[sheetData].usedRange;
Excel.PivotCache pivotcache = excelApp.ActiveWorkbook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, pivotData.Address/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
excelApp.Worksheets[sheetDest].Activate();
pivotTable1 = excelApp.Worksheets[sheetDest].PivotTables.Add(pivotcache, pivotDest, pitpivotNam1, useDefault/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
// Create Dynamically PivotFields
for (int i = 1; i <= headerNam.Length - 1; i++)
{
pFields.Add("test" + i.ToString(), (Excel.PivotField)pivotTable1.PivotFields(i));
}
pFields["test20"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields["test20"].Function = Excel.XlConsolidationFunction.xlAverage;
pFields["test43"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields["test43"].Function = Excel.XlConsolidationFunction.xlAverage;
pFields["test66"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields["test66"].Function = Excel.XlConsolidationFunction.xlAverage;
pFields["test20"].Name = "DVP & R Progress";
// First Chart
excelApp.ActiveSheet.Shapes.AddChart.Select();
excelApp.ActiveChart.SetSourceData(pivotTable1.TableRange1);
// Second PivotTable
string pitpivotNam2 = "table2";
Excel.PivotTable pivotTable2 = null;
Dictionary<String, Excel.PivotField> pFields2 = new Dictionary<String, Excel.PivotField>();
Excel.Range pivotDest2 = excelApp.Worksheets["PivotTable"].Cells[20, 1];
excelApp.Worksheets[sheetData].Activate();
int countColmn2 = excelApp.Cells[1, 1].End(Excel.XlDirection.xlToRight).Column;
var headerNam2 = excelApp.Range[excelApp.Cells[1, 1], excelApp.Cells[1, countColmn2]].Value;
// Data Source for PivotTable
Excel.Range pivotData2 = excelApp.Worksheets[sheetData].usedRange;
Excel.PivotCache pivotcache2 = excelApp.ActiveWorkbook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, pivotData2.Address/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
excelApp.Worksheets[sheetDest].Activate();
pivotTable2 = excelApp.Worksheets[sheetDest].PivotTables.Add(pivotcache2, pivotDest2, pitpivotNam2, useDefault/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
// Create Dynamically PivotFields
for (int i = 1; i <= headerNam2.Length - 1; i++)
{
pFields2.Add("test" + i.ToString(), (Excel.PivotField)pivotTable2.PivotFields(i));
}
pFields2["test17"].Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
Excel.PivotField dataField = pivotTable2.DataPivotField;
pFields2["test17"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields2["test17"].Function = Excel.XlConsolidationFunction.xlCount;
pFields2["test40"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields2["test40"].Function = Excel.XlConsolidationFunction.xlCount;
pFields2["test63"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields2["test63"].Function = Excel.XlConsolidationFunction.xlCount;
dataField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
// Secound Chart
excelApp.ActiveSheet.Shapes.AddChart.Select();
excelApp.ActiveChart.SetSourceData(pivotTable2.TableRange1);