从 Select 菜单选项创建唯一的隐藏字段 IDS

Create Unique Hidden Field IDS from Select Menu options

我有一个模式显示仓库中有多个位置的特定项目的库存信息。用户从每个菜单中选择位置和数量并单击确认,然后需要将此模式中的信息导入到打印出来的挑选列表中。

为此,我计划使用数组将数据传输到选择列表。

我的每一行都有一个隐藏字段,其中包含位置值和从那里挑选的数量。

Location 1 + Qty 1 = Hidden Field 1

Location 2 + Qty 2 = Hidden Field 2

我现在希望能够在单击按钮后将这些隐藏字段放入数组中。

Hidden Field 1 + Hidden Field 2 = Array.

我可以很好地创建隐藏字段,当我制作包含所有数据的最终数组时,它似乎只想将要创建的最新隐藏字段添加到其中。

对话框 - 选择数量按钮(用于确认选择):

//PICK QUANTITY Button
'Pick Quantity': function() {
    
jQuery('.ui-dialog button:nth-child(1)').button('disable');     
    
//Disables the current selection, so that it cannot be editted
$('#AddLocQtyPick'+Picker).prop ('disabled', true); 

//Disables the current selection, so that it cannot be editted
$('#LocationPickerSelect'+ Picker).prop ('disabled', true);  

//Adds Unique Number to the ID of the input fields
Picker++; 

//For Loop that helps to total up the quanities being selected in each picker
total=0;
for (i = 0; i<Picker; i++) {
total= total + $('#AddLocQtyPick'+i).val() * 1.0;
}

//Variable decides max value of pick on appends using previous selection
QtyReqTot= QtyReq - total; 


//"Pick Another location" button is enabled whilst Qty Req has not been met
if (total !== QtyReq){
    jQuery('.ui-dialog button:nth-child(2)').button('enable');
}

//"Pick Quantity", "Pick Another Location" are disabled, whilst "Confirm" button is enabled when total reaches Qty Req
if (total == QtyReq){
    jQuery('.ui-dialog button:nth-child(2)').button('disable');
    jQuery('.ui-dialog button:nth-child(1)').button('disable');
    jQuery('.ui-dialog button:nth-child(3)').button('enable');
}

//Pick Another Location button is disabled if no more locations to pick from
if (length == 1){ 
    jQuery('.ui-dialog button:nth-child(2)').button('disable');
}

if (total !== QtyReq && length == 1){
    jQuery('.ui-dialog button:nth-child(1)').button('disable');
    $(":button:contains('Cancel')").focus();
}



//Create Hidden Field - Location

//for loop that creates the fields

for (i = 0; i<Picker; i++){
HiddenSelection = [$('#LocationPickerSelect'+i).val(),$('#AddLocQtyPick'+i).val()];
var appendHiddenSelection = '<input type="hidden" class="HiddenSelection'+ i +'" value='+HiddenSelection+'>';
$('#AddLocationPicker').append(appendHiddenSelection);  
alert(appendHiddenSelection +'This is SelectionField'+i);

}
},

确认按钮 - 用于生成包含先前数组的最终数组:

'Confirm': function() {     

//Reset the length loop
length = undefined;

//Remove "Multiple Location" icon from the row.
$('#icon'+id).hide();

//Checks "Multiple Location" icon for existence and adds Pick List button when all hidden.
$('img[id^=icon]:visible').length || $('#ProcessPickList').show();

//Change text colour back to blue to have visual confirmation that item is ready for picking
$('#Desc'+id).css('color', '#0000FF');
$('#QtyReq'+id).css('color', '#0000FF');
$('#QtyinStock'+id).css('color', '#0000FF');

//Create Total Array

TotalHiddenArray = [HiddenSelection]

alert (TotalHiddenArray);


$(this).dialog('close');
    
},

我想我需要能够为输入字段创建唯一的 IDS 并展示如何将它们全部添加到数组中。

您可以尝试更换

HiddenArray = [appendHiddenQty, appendHiddenLocation]

来自

HiddenArray[HiddenArray.length] = [appendHiddenQty, appendHiddenLocation]

这样,您只需在 HiddenArray 的末尾添加 [appendHiddenQty, appendHiddenLocation],而不是在循环中覆盖 HiddenArray


编辑 1:

替换

HiddenSelection = [$('#LocationPickerSelect'+i).val(),$('#AddLocQtyPick'+i).val()];

来自

HiddenSelection[HiddenSelection.length] = [$('#LocationPickerSelect'+i).val(),$('#AddLocQtyPick'+i).val()];

或者,您也可以使用 push :

HiddenSelection.push([$('#LocationPickerSelect'+i).val(),$('#AddLocQtyPick'+i).val()]);

Please see this quickly made Fiddle


编辑 2:

好的,让我们尝试将整个循环替换为:

var HiddenSelection = new Array;
for (i = 0; i<Picker; i++){
    HiddenSelection = [$('#LocationPickerSelect'+i).val(),$('#AddLocQtyPick'+i).val()];
    var appendHiddenSelection = '<input type="hidden" class="HiddenSelection'+ i +'"     value='+HiddenSelection+'>';
    $('#AddLocationPicker').append(appendHiddenSelection);  
    alert(appendHiddenSelection +'This is SelectionField'+i);

    TotalHiddenArray.push([HiddenSelection]);
}

您只需将其从 confirm 函数中删除即可:

//Create Total Array
TotalHiddenArray = [HiddenSelection]

你还必须将 TotalHiddenArray 作为任何函数之外的新数组(例如,在你的 JS 代码的最顶部,因为我猜你正试图从另一个函数访问 TotalHiddenArray 而不是循环)像这样:

var TotalHiddenArray= new Array;

Another Fiddle