gpt4 book ai didi

javascript - 使用javascript函数获取html控件值

转载 作者:行者123 更新时间:2023-11-28 17:33:20 25 4
gpt4 key购买 nike

我的页面中有这个表,我通过使用 AJAX 请求来获取它。

<table>
<tbody>
<tr>
<th>No</th>
<th>Name</th>
<th>Action</th>
</tr>
<tr>
<td>1</td>
<td>Example Name 1</td>
<td class="text-center">
<div class="btn-group" role="group" aria-label="">
<a href="javascript:;" onclick="deleteParticipantFunction()" class="delete-participant" value="2">
Delete
</a>
</div>
</td>
</tr>
<tr>
<td>2</td>
<td>Example Name 2</td>
<td class="text-center">
<div class="btn-group" role="group" aria-label="">
<a href="javascript:;" onclick="deleteParticipantFunction()" class="delete-participant" value="1">
Delete
</a>
</div>
</td>
</tr>
</tbody>
</table>

所以我试图在单击按钮时获取 anchor 值。所以我做了一个这样的函数:

function deleteParticipantFunction(){
var result = confirm('You are about to delete this participant. Continue?');
if(result){
var participant_id = $(this).val();
console.log(participant_id);
}
};

但我无法获取该值。在函数中获取值的正确方法是什么?

最佳答案

将 id 直接传递给方法:

<a href="javascript:;" onclick="deleteParticipantFunction(1)" class="delete-participant">

那么你的js方法会更简单:

function deleteParticipantFunction(participant_id){
var result = confirm('You are about to delete this participant. Continue?');
if(result){
//delete smth by id
}
};

关于javascript - 使用javascript函数获取html控件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49793995/

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