From 3b15b1ceb40e303913f4c396f02aec9838574cc2 Mon Sep 17 00:00:00 2001
From: hwh <332078369@qq.com>
Date: 星期三, 19 六月 2024 14:28:15 +0800
Subject: [PATCH]
---
HTML/js/public.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 51 insertions(+), 0 deletions(-)
diff --git a/HTML/js/public.js b/HTML/js/public.js
index a15a788..27b625e 100644
--- a/HTML/js/public.js
+++ b/HTML/js/public.js
@@ -282,4 +282,55 @@
});
});
}
+}
+//娣辨嫹璐�
+function deepClone(source) {
+ if (typeof source !== 'object' || source == null) {
+ return source;
+ }
+ const target = Array.isArray(source) ? [] : {};
+ for (const key in source) {
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
+ if (typeof source[key] === 'object' && source[key] !== null) {
+ target[key] = deepClone(source[key]);
+ } else {
+ target[key] = source[key];
+ }
+ }
+ }
+ return target;
+}
+/**
+ * 灏嗚〃鍗曡祴鍊间负鎸囧畾鐨勫璞�
+ * @param {Object} data - 鍖呭惈琛ㄥ崟鏁版嵁鐨勫璞�
+ * @param {String} formSelector - 琛ㄥ崟鐨勯�夋嫨鍣紝渚嬪 '#myForm' 鎴� '.myForm'
+ */
+function setFormData(data, formSelector) {
+ var $form = $(formSelector);
+
+ $.each(data, function(key, value) {
+ var $field = $form.find('[name=' + key + ']');
+
+ if ($field.length > 0) {
+ var fieldType = $field.attr('type');
+
+ switch (fieldType) {
+ case 'checkbox':
+ if (Array.isArray(value)) {
+ $field.each(function() {
+ $(this).prop('checked', value.includes($(this).val()));
+ });
+ } else {
+ $field.prop('checked', value);
+ }
+ break;
+ case 'radio':
+ $field.filter('[value=' + value + ']').prop('checked', true);
+ break;
+ default:
+ $field.val(value);
+ break;
+ }
+ }
+ });
}
\ No newline at end of file
--
Gitblit v1.8.0