[英]Alpaca JS is not properly reordering array checkboxes
我有一個Alpaca JS表單,由一組項目組成,每個項目由一個文本框和一個復選框組成。 出於某種原因,當我使用動態控件更改順序時,它成功地對文本框重新編號,但不會更改復選框的編號。 如果按下動態添加新字段的相同頂部按鈕,這也會導致分配重復名稱。 最終結果是提交表單時傳遞的數據不正確。 如何解決此問題以正確重新編號復選框?
這是 Alpaca 配置的示例:
$("#form1").alpaca({
"schema": {
"title": "Testing checkbox array IDs",
"description": "Testbox checkbox array test.",
"type": "object",
"properties": {
"form-fields": {
"title": "Fields",
"description": "These are the fields.",
"type": "array",
"items": {
"type": "object",
"properties": {
"field-name": {
"type": "string",
"title": "Field Name",
"description": "Enter the name for this field.",
"required": true
},
"field-box": {
"type": "boolean",
"title": "Field Box",
"description": "Check this box.",
"default": false
}
}
}
}
}
}
});
我找不到糾正行為本身的方法,但我能夠通過向 Alpaca 定義添加 postRender 事件來解決它,如下所示:
"postRender": function(control) {
control.childrenByPropertyId["form-fields"].on("move", function() { $('input[type=checkbox]').each(function(index) { $(this).attr("name", $(this).closest("div:has(*[name])").first().attr("name")) }); });
control.childrenByPropertyId["form-fields"].on("add", function() { $('input[type=checkbox]').each(function(index) { $(this).attr("name", $(this).closest("div:has(*[name])").first().attr("name")) }); });
control.childrenByPropertyId["form-fields"].on("remove", function() { $('input[type=checkbox]').each(function(index) { $(this).attr("name", $(this).closest("div:has(*[name])").first().attr("name")) }); });
}
這是一個小技巧,但它有效,因為父對象確實被分配了正確的名稱值,並且如果名稱只是被復制到輸入元素中,表單將使用這些值發布。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.