gpt4 book ai didi

javascript - 更改 foreach 循环内的按钮文本

转载 作者:行者123 更新时间:2023-12-03 03:30:17 47 4
gpt4 key购买 nike

我有一个按钮,当我单击该按钮时,它应该更改它的文本。它位于 foreach 循环内。但问题是,当我单击该按钮时,第一个按钮的文本会发生变化。我想更改我单击的特定按钮文本..我该怎么做?

前端部分

@foreach($products as $product) 
<button onclick="this.disabled=true; quoteAdd('{!!$product->id!!}')" id="inquireButton" class="btn btn-danger ">Inquire</button>
@endforeach

Javascript部分

function quoteAdd(productId)
{
$("#inquireButton").html('Save');
// other code ..
}

最佳答案

您需要使用 this 关键字将按钮引用传递给 quoteAdd 方法,如下所示。

PHP

@foreach($products as $product) 
<button onclick="this.disabled=true; quoteAdd('{!!$product->id!!}', this)" id="inquireButton" class="btn btn-danger ">Inquire</button>
@endforeach

JS

function quoteAdd(productId, button) {
$(button).text('Save');
//other code ..
}

仅供引用 id 对于每个元素应该是唯一的。

关于javascript - 更改 foreach 循环内的按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46133542/

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