javascript - 如何使用javascript捕获多个表单的点击事件

标签 javascript html css forms

一个页面上会有0-10个表格。每个都将允许用户对该表单的内容进行投票。我如何利用 javascript 捕获那些不同表单的点击事件,并将内容返回到特定表单的范围内。如果我为表单动态生成 ID,我该如何在 JavaScript 中对此进行说明?

最佳答案

如果您使用 jQuery JavaScript 库,那么您可以一次性将相同的函数附加到所有表单,并在回调函数中返回对表单的引用。例如,如果您正在等待用户提交表单以发送 AJAX 请求,您可以这样做:

// `$('form')` selects all the forms on the page
// `.submit();` will call a function when the submit event is fired on all forms
$('form').submit(function() {
    // Store a reference to this form
    var $thisForm = $(this);

    $.ajax({
        ...
        success: function() {
            // do something with the $thisForm variable which is still in scope.
        }
    });
});

关于javascript - 如何使用javascript捕获多个表单的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4629313/

相关文章:

javascript - 进行 Javascript 函数调用监听器/按键的最灵敏的方式是什么

php - 试图弄清楚为什么这个 PHP while 循环实际上有效

html - 在更改工具中的值之前,填充不会应用于伪元素

html - 在 ngFor 列表 Angular 中将一张图片放在另一张图片下方

css - svg 中跨度的 Xpath

javascript - 如何显示成功消息 NgRX Effects 和调度事件

javascript - 如何清除 session cookie

javascript - 创建悬停可见按钮

html - 如何将图像居中放置在div的中间?

javascript - javascript 输出对象数组