繁体   English   中英

更改ajax成功回调上的按钮文本

[英]Change text of button on ajax success callback

我一直在努力更改触发我的ajax请求以显示数据已成功发送到服务器的按钮的文本。

标记

使用laravel,我有这种形式:

{{ Form::open(array('url' => 'panel/update/user', 'style' => 'display:inline', 'id' => 'ajax')) }}

    {{ Form::hidden('action', 'confirm') }}
    {{ Form::hidden('user', $confirmed->username) }}

      <button class="btn-xs btn-default pull-right btn" style="padding: 1px 5px;" type="submit" data-after="User Confirmed">Confirm User</button>

{{ Form::close() }}

对于javascript:

$('form#ajax').on('submit', function(){
    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[name]').each(function(index, value){
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

        data[name] = value;
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response){

            $('#console p').text(response); // This is logging the response from the server in a little console I built.



            /*
            | If data-after is set, then set the value to the button that triggered this 
            */


            if(this.attr('data-after').length) {

                console.log(this);

            }



        }
    });

    return false;
});

ID为#ajax任何表单都将与ajax一起提交。 我现在正在尝试实现一种方法,该方法指定一些数据属性以向触发提交以显示表单已提交的当前按钮显示文本。 因此, data-after="Submitted"将成功地从Confirm => Submitted更改文本。 我希望这种形式,而不是全部更改。

问题

不是我在页面上的唯一表格。 目的是从数据库中获取未确认的用户,然后单击通过ajax将数据发送到服务器的按钮来确认他们。 发送数据后,我想将按钮中的文本更改为data-after指定的文本。 我有多个带有多个数据后的表格,所以我不想更改所有按钮。 这就是我卡住的地方。 我想证明表格已提交。

您可以将按钮定位为Submit事件的开始,然后在success方法中引用它。

缩写示例:

$('form#ajax').on('submit', function() {
        var $button = $(this).find('button');

        $.ajax({
            url: url,
            type: type,
            data: data,
            success: function(response) {
                // button manipulation here
                $button.attr('disabled', 'disabled').text('Submitted');
            }
        });

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM