18910140161

JavaScript-如何以我想要的方式创建列表-堆栈溢出

顺晟科技

2022-10-19 12:14:46

76

我有以下脚本:

为我做计算并列出它们

function fromPortIdChange() {
   fromportid = $('#fromPortId').val();
   console.log(fromportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function toPortIdChange() {
   toportid = $('#toPortId').val();
   console.log(toportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function portIdRange(fromportid, toportid) {
   fromportid = $('#fromPortId').val();
   toportid = $('#toPortId').val();
   fromportidlastvalueSliced = fromportid.slice(-5);
   interfaceNameLength = fromportid.length-1;
   interfaceName = fromportid.substring(0, interfaceNameLength);
   console.log("length: " +interfaceNameLength);
   console.log("interface name: " +interfaceName);
   fromportidlastvalue = fromportidlastvalueSliced.split('/')[2]
   console.log(fromportidlastvalue+ " FROM port id"); 
   console.log("range is " +fromportid+ " to " +toportid);
   toportidlastvalueSliced = toportid.slice(-5);
   toportidlastvalue = toportidlastvalueSliced.split('/')[2]
   console.log(toportidlastvalue+ " TO port id"); 
   console.log(typeof(toportidlastvalue));
   calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName);
}

function calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName) {
   fromportidlastvalue = parseInt(fromportidlastvalue);
   toportidlastvalue = parseInt(toportidlastvalue);
   console.log(fromportidlastvalue + " type is  " + typeof(fromportidlastvalue));
   console.log(toportidlastvalue + " type is  " + typeof(toportidlastvalue));
   currentValue = fromportidlastvalue;
   while(currentValue<=toportidlastvalue)
   {
      console.log(currentValue);
      listOfNumbers.push(interfaceName+currentValue);
      currentValue++;                
   }
   $('#port_range').val(listOfNumbers);
}

以下是所涉及的html:

页面上的部分和我要克隆的部分

function fromPortIdChange() {
   fromportid = $('#fromPortId').val();
   console.log(fromportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function toPortIdChange() {
   toportid = $('#toPortId').val();
   console.log(toportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function portIdRange(fromportid, toportid) {
   fromportid = $('#fromPortId').val();
   toportid = $('#toPortId').val();
   fromportidlastvalueSliced = fromportid.slice(-5);
   interfaceNameLength = fromportid.length-1;
   interfaceName = fromportid.substring(0, interfaceNameLength);
   console.log("length: " +interfaceNameLength);
   console.log("interface name: " +interfaceName);
   fromportidlastvalue = fromportidlastvalueSliced.split('/')[2]
   console.log(fromportidlastvalue+ " FROM port id"); 
   console.log("range is " +fromportid+ " to " +toportid);
   toportidlastvalueSliced = toportid.slice(-5);
   toportidlastvalue = toportidlastvalueSliced.split('/')[2]
   console.log(toportidlastvalue+ " TO port id"); 
   console.log(typeof(toportidlastvalue));
   calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName);
}

function calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName) {
   fromportidlastvalue = parseInt(fromportidlastvalue);
   toportidlastvalue = parseInt(toportidlastvalue);
   console.log(fromportidlastvalue + " type is  " + typeof(fromportidlastvalue));
   console.log(toportidlastvalue + " type is  " + typeof(toportidlastvalue));
   currentValue = fromportidlastvalue;
   while(currentValue<=toportidlastvalue)
   {
      console.log(currentValue);
      listOfNumbers.push(interfaceName+currentValue);
      currentValue++;                
   }
   $('#port_range').val(listOfNumbers);
}

中为克隆创建的附加div
function fromPortIdChange() {
   fromportid = $('#fromPortId').val();
   console.log(fromportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function toPortIdChange() {
   toportid = $('#toPortId').val();
   console.log(toportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function portIdRange(fromportid, toportid) {
   fromportid = $('#fromPortId').val();
   toportid = $('#toPortId').val();
   fromportidlastvalueSliced = fromportid.slice(-5);
   interfaceNameLength = fromportid.length-1;
   interfaceName = fromportid.substring(0, interfaceNameLength);
   console.log("length: " +interfaceNameLength);
   console.log("interface name: " +interfaceName);
   fromportidlastvalue = fromportidlastvalueSliced.split('/')[2]
   console.log(fromportidlastvalue+ " FROM port id"); 
   console.log("range is " +fromportid+ " to " +toportid);
   toportidlastvalueSliced = toportid.slice(-5);
   toportidlastvalue = toportidlastvalueSliced.split('/')[2]
   console.log(toportidlastvalue+ " TO port id"); 
   console.log(typeof(toportidlastvalue));
   calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName);
}

function calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName) {
   fromportidlastvalue = parseInt(fromportidlastvalue);
   toportidlastvalue = parseInt(toportidlastvalue);
   console.log(fromportidlastvalue + " type is  " + typeof(fromportidlastvalue));
   console.log(toportidlastvalue + " type is  " + typeof(toportidlastvalue));
   currentValue = fromportidlastvalue;
   while(currentValue<=toportidlastvalue)
   {
      console.log(currentValue);
      listOfNumbers.push(interfaceName+currentValue);
      currentValue++;                
   }
   $('#port_range').val(listOfNumbers);
}

此部分用于textarea

function fromPortIdChange() {
   fromportid = $('#fromPortId').val();
   console.log(fromportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function toPortIdChange() {
   toportid = $('#toPortId').val();
   console.log(toportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function portIdRange(fromportid, toportid) {
   fromportid = $('#fromPortId').val();
   toportid = $('#toPortId').val();
   fromportidlastvalueSliced = fromportid.slice(-5);
   interfaceNameLength = fromportid.length-1;
   interfaceName = fromportid.substring(0, interfaceNameLength);
   console.log("length: " +interfaceNameLength);
   console.log("interface name: " +interfaceName);
   fromportidlastvalue = fromportidlastvalueSliced.split('/')[2]
   console.log(fromportidlastvalue+ " FROM port id"); 
   console.log("range is " +fromportid+ " to " +toportid);
   toportidlastvalueSliced = toportid.slice(-5);
   toportidlastvalue = toportidlastvalueSliced.split('/')[2]
   console.log(toportidlastvalue+ " TO port id"); 
   console.log(typeof(toportidlastvalue));
   calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName);
}

function calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName) {
   fromportidlastvalue = parseInt(fromportidlastvalue);
   toportidlastvalue = parseInt(toportidlastvalue);
   console.log(fromportidlastvalue + " type is  " + typeof(fromportidlastvalue));
   console.log(toportidlastvalue + " type is  " + typeof(toportidlastvalue));
   currentValue = fromportidlastvalue;
   while(currentValue<=toportidlastvalue)
   {
      console.log(currentValue);
      listOfNumbers.push(interfaceName+currentValue);
      currentValue++;                
   }
   $('#port_range').val(listOfNumbers);
}

RightTable来自我的django视图

此脚本执行以下操作:用户从下拉框和下拉框中选择,它将列出所有可能的接口,如图中所示,它列出

如您所见,侧边有一个添加按钮。这是为了克隆下拉框,让用户选择其他端口。例如克隆的和。因此,将以下内容添加到

的文本区域中

下拉框的克隆:

function fromPortIdChange() {
   fromportid = $('#fromPortId').val();
   console.log(fromportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function toPortIdChange() {
   toportid = $('#toPortId').val();
   console.log(toportid);
   portIdRange(fromportid,toportid);
   listOfNumbers=[];
}

function portIdRange(fromportid, toportid) {
   fromportid = $('#fromPortId').val();
   toportid = $('#toPortId').val();
   fromportidlastvalueSliced = fromportid.slice(-5);
   interfaceNameLength = fromportid.length-1;
   interfaceName = fromportid.substring(0, interfaceNameLength);
   console.log("length: " +interfaceNameLength);
   console.log("interface name: " +interfaceName);
   fromportidlastvalue = fromportidlastvalueSliced.split('/')[2]
   console.log(fromportidlastvalue+ " FROM port id"); 
   console.log("range is " +fromportid+ " to " +toportid);
   toportidlastvalueSliced = toportid.slice(-5);
   toportidlastvalue = toportidlastvalueSliced.split('/')[2]
   console.log(toportidlastvalue+ " TO port id"); 
   console.log(typeof(toportidlastvalue));
   calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName);
}

function calculateRange(fromportidlastvalue, toportidlastvalue, interfaceName) {
   fromportidlastvalue = parseInt(fromportidlastvalue);
   toportidlastvalue = parseInt(toportidlastvalue);
   console.log(fromportidlastvalue + " type is  " + typeof(fromportidlastvalue));
   console.log(toportidlastvalue + " type is  " + typeof(toportidlastvalue));
   currentValue = fromportidlastvalue;
   while(currentValue<=toportidlastvalue)
   {
      console.log(currentValue);
      listOfNumbers.push(interfaceName+currentValue);
      currentValue++;                
   }
   $('#port_range').val(listOfNumbers);
}

出现了问题:

1.添加按钮可以工作,但我不知道它是滞后还是什么。有时,在出现克隆的下拉框之前,我必须多次单击添加按钮。

2.克隆的下拉框并不像我希望的那样工作。就像下图中那样。在第一排我们有和。中列出了。但是对于and的第2行,它没有在

中列出

很抱歉发了一篇很长的帖子,但无论如何我都能实现我需要的东西。如果有人能帮忙,我很感激。提前感谢!


顺晟科技:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航