【问题标题】:Create Unique Hidden Field IDS from Select Menu options从选择菜单选项创建唯一的隐藏字段 ID
【发布时间】:2015-12-18 04:15:46
【问题描述】:

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

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

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

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

【问题讨论】:

  • TotalHiddenArray = [HiddenArray]”。 HiddenArray 的声明和(可能)填写在哪里?你能让你的代码更清晰一点吗?
  • @NaijaProgrammer 抱歉,我现在稍微更改了代码,并编辑了我的问题。用于数组的变量在“Pick Quantity”部分中声明。
  • 到底是什么问题,您的代码的哪一部分有问题。是否可以将其简化为该部分?顺便说一句,HiddenSelection 是一个数组,您将其设置为表单输入字段的值。这对你有什么作用?
  • 模式获取 Location 和 Qty 的值并将它们放在隐藏字段中。它对存在的每一行都执行此操作。然后,我想获取所有这些隐藏字段(每行生成 1 个)并将它们放入一个数组中。但是,当我尝试这样做时,该数组仅包含创建的最后一个隐藏字段。我想我需要让每个隐藏字段都有一个唯一的 id,然后在创建数组时循环遍历所有字段并将它们添加到数组中?
  • 你必须看看 Edensource 发布的答案。这应该可以解决您的问题。

标签: javascript php jquery arrays hidden-field


【解决方案1】:

你可以试试替换

HiddenArray = [appendHiddenQty, appendHiddenLocation]

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

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


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

请看这个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]

您还必须在任何函数之外将TotalHiddenArray 作为新数组删除(例如,在您的 JS 代码的最顶部,因为我猜您正试图从循环之外的另一个函数访问TotalHiddenArray),如下所示:

var TotalHiddenArray= new Array;

另一个Fiddle

【讨论】:

  • 感谢@EdenSource 的回复,我想在有人发布答案之前我有时间进行快速编辑,因为我已经稍微更改了我的问题和代码。再看看,如果你能提供帮助,请告诉我。
  • 提示应该是一样的,请看答案中的编辑。
  • 不喜欢那样,说当我做那个改变时没有定义 HiddenSelection。
  • 您必须在向其中推送新元素之前声明您的数组:var HiddenSelection = new Array;
  • 很高兴它有帮助。请询问是否还有其他问题;)
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多