【问题标题】:jQuery - Handle a specific form among so many othersjQuery - 处理许多其他表单中的特定表单
【发布时间】:2012-12-25 22:38:43
【问题描述】:

我知道很多人为此创建了主题:“在同一页面上处理多个表单”。然而,在阅读了大部分之后,我没有找到解决方案。

我有一个列出一些文章的页面。借助滑块,用户可以在每个帖子上打分,然后提交。 为了提交与文章关联的标记,我使用了函数 .each() 但我没有得到预期的结果:它是我提交的页面的最后一个表单,无论我提交什么表单。

HTML:

<form method="post" action="" class="vote-form">
    <input type="hidden" id="amount" name="amount" class="hidden" value="5" />
    <input type="hidden" id="post_id" name="post_id" value="<?php echo get_the_ID(); ?>" />
    <input type="submit" name="post_vote" value="VOTE">
</form>

JS:

$( ".vote-form" ).each(function() {

    $( this ).submit(function() {

        // get all the inputs into an array.
        var $inputs = $('.vote-form :input');

        // get an associative array of just the values.
        var values = {};
        $inputs.each(function() {
            values[this.name] = $(this).val();
            alert(values[this.name]);
        });

        // ************ Save vote in AJAX *************
        // ...

        return false;
    });
});

当我提交一个表单时,警报会显示隐藏输入的每个值。

【问题讨论】:

    标签: jquery submit selector multiple-forms


    【解决方案1】:

    您需要为确定的元素获取inputs。你可以使用.find()

    $( ".vote-form" ).each(function() {
        var $this = $(this);
        $this.submit(function() {
            // get all the inputs into an array.
            var $inputs = $this.find(':input');
            ...
    

    您正在抓取所有现有的 input 元素,我怀疑因此在创建关联数组期间,具有相同 this.name 的元素在您处理匹配元素列表时被覆盖。

    顺便说一句,您可以删除.each(),因为.submit() 对匹配的元素集起作用。

    注意:我在这里缓存了$(this)

    【讨论】:

    • 感谢您的清晰解释。这似乎很好:) 捕捉 $(this) 有什么好处?
    • @AdrienG,你的意思是“缓存”。更少的 DOM 调用
    • 是的,抱歉...我添加此评论时已经晚了。好的很高兴知道!
    【解决方案2】:

    显然它会显示所有数据,因为您在类vote-form 的所有元素上使用each() 循环。如果您只想显示提交的表单,只需删除 each 并使用单个提交事件触发器,例如:

    $('.vote-form').submit(function() {
    
        // get all the inputs into an array.
        var $inputs = $(this).find(':input'); // Get all input fields of "this" specific form
    
        // get an associative array of just the values.
        var values = {};
        $inputs.each(function() {
            values[this.name] = $(this).val();
            alert(values[this.name]);
        });
    
        // ************ Save vote in AJAX *************
        // ...
    
        return false;
    });
    

    【讨论】:

    • @Alexander Ehhhh 是的,这太草率了。我的坏,修好了。感谢您指出。
    • 我的反对票已锁定。无论如何,您的论点似乎不正确:|
    【解决方案3】:

    使用主要父元素中的find(),在您的情况下是表单,您将搜索隔离到您正在使用的特定实例

     var $inputs = $(this).find(':input');
    

    【讨论】:

      【解决方案4】:

      你的问题是上下文。 通过选择您的输入

      var $inputs = $('.vote-form :input');
      

      您不会告诉 jQuery 特定的表单,因此它会选择所有具有“.vote-form”类的输入。我更喜欢在 jQuery-Selector 中传递上下文。 所以它应该是这样的:

      var $inputs = $(':input', $(this));
      

      通过使用this,您可以在提交方法中获取表单对象。

      工作 js-fiddle:http://jsfiddle.net/xuAQv/282/

      【讨论】:

      • $('.vote-form :input', $(this)) 不起作用,$('.vote-form :input', this) 也一样 :)
      • @Alexander 不知道你为什么说它不起作用......不管额外的$() 是否有效
      • @charlietfl,你应该试试。我认为这样更快
      • 它可以工作,但我的代码有一点错误。您只需要选择 ':input' 因为它只选择上下文的子项。并且表单 $(this) 没有 .vote-form 类的子级。
      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多