gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:27:01 25 4
gpt4 key购买 nike

我一直在努力更改触发我的 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 更改文本。我想要这个 per form,而不是改变所有。

问题

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

最佳答案

您可以在提交事件的开头定位按钮,然后在 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');
}
});

});

关于javascript - 更改 ajax 成功回调按钮的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24047179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com