webcito/jquery-form 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webcito/jquery-form

最新稳定版本:v1.0.5

Composer 安装命令:

composer require webcito/jquery-form

包简介

The easy way to handle forms with jQuery and Bootstrap

README 文档

README

Version License jQuery Bootstrap

A powerful, lightweight jQuery plugin designed to streamline form handling with AJAX and Bootstrap 5. It automates validation display, file uploads, and provides a rich API for modern web applications.

changelog

🚀 Key Features

  • Seamless AJAX Integration: Automatic form submission and state management.
  • Bootstrap 5 Native: Built-in support for Bootstrap validation states (is-invalid, invalid-feedback).
  • Smart File Uploads: Automatic detection of file inputs with native FormData support.
  • Real-time Progress: Built-in upload progress tracking for responsive UIs.
  • Asynchronous Workflow: Support for async/await in pre-submission hooks.
  • Auto-Initialization: Zero-config initialization via HTML5 data attributes.
  • Enhanced UX: Automatic "Required" field indicators and modal integration.
  • Event-Driven Architecture: Comprehensive event system for maximum extensibility.

📋 Requirements

Dependency Version
jQuery >= 3.6
Bootstrap >= 5.0

📦 Installation

Via Composer

composer require webcito/jquery-form

Manual Installation

Download the latest release and include the script in your project:

<script src="dist/jquery.form.min.js"></script>

Automatic initialization via Data Attributes

The plugin automatically initializes for forms with data-bs-toggle="form" or data-toggle="form". Initialization occurs immediately on page load thanks to a MutationObserver, as well as for any forms added later (e.g., via AJAX). For batch operations, initialization is performed efficiently using debouncing.

<form action="/api/submit" method="POST" data-bs-toggle="form">
    <div class="mb-3">
        <label for="user_email" class="form-label">Email address</label>
        <input type="email" name="email" id="user_email" class="form-control" required>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

2. JavaScript Initialization

For more control, initialize the plugin manually:

$('.my-form').form({
    onSuccess: function (form, response) {
        alert('Form submitted successfully!');
    }
});

⚙️ Configuration

Use $.form.setDefaults() to configure global behavior or pass options during initialization.

Option Type Default Description
autocomplete boolean false Globally disables browser autocomplete/autocorrect.
resetOnModalHidden boolean true Resets the form when its parent Bootstrap modal is closed.
setErrorsOnElements boolean true Automatically places the errors in the input fields.
onBeforeSend function null Hook before submission. Supports async. Return false to abort.
onSuccess function null Triggered on successful AJAX response.
onError function null Triggered when the server returns errors.
onProgress function null Provides upload progress (0-100).
onComplete function null Triggered after success or error.

📤 File Uploads & Progress Tracking

The plugin handles multi-part form data automatically. Implementing a progress bar is trivial:

$('#uploadForm').form({
    onProgress: function (form, progress) {
        const percent = Math.round(progress) + '%';
        $('.progress-bar').css('width', percent).text(percent);
    }
});

⚡ Events API

The plugin triggers namespaced events (.bs.form) for deep integration. Events can be listened to on the form element or via delegation.

Form Events

Event Parameters Description
init.bs.form event, $form Triggered after initialization.
beforeSend.bs.form event, $form, aborted Before AJAX request. aborted is true if onBeforeSend returned false.
progress.bs.form event, $form, progress Upload progress (0-100).
success.bs.form event, $form, response On successful AJAX response.
error.bs.form event, $form, errors, jqXHR On AJAX error or validation failure.
complete.bs.form event, $form, response After request completion (success or error).
cleared.bs.form event, $form Triggered when validation states are removed.
resetting.bs.form event, $form Triggered on form reset.
any.bs.form event, eventName Special event triggered for every plugin action.

Field Events

Event Parameters Description
error.bs.form event, $field, message Triggered on a specific input field when it receives an error.
$(document).on('success.bs.form', '#my-form', function (event, $form, response) {
    console.log('Server response:', response);
});

$(document).on('error.bs.form', 'input', function (event, $field, message) {
    console.warn(`Validation failed for ${$field.attr('name')}: ${message}`);
});

🛠 Public Methods

Call methods using the syntax: $(selector).form('methodName', parameters).

Method Parameters Description
setErrors object Manually display errors on specific fields. Keys must match field names.
// Manually set errors
$('#my-form').form('setErrors', {
    email: 'Invalid domain',
    password: 'Too weak'
});

❌ Error Handling

The plugin expects a JSON response for validation errors (HTTP 4xx or 5xx).

Field-Level Errors

Map the input name attribute to the error message:

{
  "email": "This email is already registered.",
  "username": "Username is too short."
}

Global Errors

Use the default key to display a global Bootstrap alert at the top of the form:

{
  "default": "A critical system error occurred. Please try again later."
}

🎨 Styling

The plugin injects minimal CSS to handle the required field indicators:

  • Required labels get a .required class.
  • A red asterisk is displayed via the :before pseudo-element.

⚖️ License

Proprietary. Developed by Thomas Kirsch.

统计信息

  • 总下载量: 76
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2023-09-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固