jquery - 更改输入 :hidden tag not working - jQuery 中名称 attr 的值

标签 jquery ajax forms attr

我有以下表单(如下),其中有一个隐藏的输入标签。在表单提交和响应返回时,我想将该隐藏输入名称的名称更改为具有值“__REMOVE__”。然后,在再次提交表单响应时,我希望它返回到“__ADD__”,依此类推。然而,该代码对我不起作用,名称=“__ADD__”在表单上永远不会改变。我对 jQuery 很陌生,所以请原谅我的无知。请注意,__BRONZE__ 的值永远不应更改 - 名称更改是后端 PHP 的要求。

表格:

<form action="cart.php" method="post" class="cart-ajax">
    <input type="hidden" name="__ADD__" value="__BRONZE__" />
    <br><button type="submit" class="add-more-top dark cart-button-eight">Add to Cart</button>
    <div class="cart-ajax-response"></div>
</form>

jQuery:

$(document).ready(function() {

// code here snipped out to keep this question short

    // Shopping Cart Form
    $("form.cart-ajax").submit(function(e) {
            e.preventDefault();
            var form = $(this);

            // update the submit buttons text on form submission
            if(form.find('input:hidden').attr('name', '__ADD__'))
            {
                    form.find('button:submit').html('Adding...');
            }
            else if(form.find('input:hidden').attr('name', '__REMOVE__'))
            {
                    form.find('button:submit').html('Removing...');
            }

            $.post(form.attr('action'), form.serialize(),
                    function(data) {

                            // update the submit buttons text after successful response 
                            // update the hidden form field with the opposite of the current value
                            if(form.find('input:hidden').attr('name') == '__ADD__')
                            {
                                    form.find('button:submit').html('Remove');
                                    form.find('input:hidden').attr('name', '__REMOVE__');
                            }
                            else if(form.find('input:hidden').attr('name') == '__REMOVE__')
                            {
                                    form.find('button:submit').html('Add');
                                    form.find('input:hidden').attr('name', '__ADD__');
                            }

                            // do something with the returned data - used in cart-ajax-response div
                            form.find('.cart-ajax-response').empty().html(data.aResults[0]);

                    }, 'json');
    });
}); // <-- document ready

最佳答案

if(form.find('input:hidden').attr('name', '__ADD__'))

没有按照你的想法去做。您实际上是在设置该字段的名称,而不是询问它是否存在。

使用

if(form.find('input[name="__ADD__"]').length)

并注意.length为您提供一个整数。如果没有它,您将返回一个对象,即使其长度为 0,这也是如此。

但是,看看这个 http://jsfiddle.net/3xTfX/2/ - 同样的事情,更少的代码

关于jquery - 更改输入 :hidden tag not working - jQuery 中名称 attr 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064116/

相关文章:

C# 与 Selenium Ajax DropdownList 问题

php - 复选框默认为选中状态,除非用户未选中

jquery - 使用 jquery 通过单选按钮切换禁用状态

forms - 过滤日期字段 + 5 年

javascript - 以编程方式更改 jQuery 文件上传 (blueimp) 的 previewMaxWidth/Height

javascript - 为什么这个函数在 jquery 1.9.1 中不再起作用?

javascript - 使用两个中间 $.getJSON 调用将字符串数组转换为对象数组

javascript - 多个 JS 文件中的 JQuery 和变量

javascript - 重定向到带有加载动画的页面

php - jquery ajax提交表单上传图片