从“选择菜单”选项创建唯一的隐藏字段IDS

时间:2015-09-21 12:46:57

标签: javascript php jquery arrays hidden-field

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

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

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

  

位置1 +数量1 =隐藏字段1

     

位置2 +数量2 =隐藏字段2

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

  

隐藏字段1 +隐藏字段2 =数组。

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

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

//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,并展示如何将它们全部添加到数组中。

1 个答案:

答案 0 :(得分:2)

您可以尝试替换

HiddenArray = [appendHiddenQty, appendHiddenLocation]

通过

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

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

EDIT1:

替换

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()]);
  

请参阅此quickly made Fiddle

EDIT2:

好的,让我们尝试用以下方法替换整个循环:

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]

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

var TotalHiddenArray= new Array;
  

另一个Fiddle