dtyw10299 2018-04-12 10:17
浏览 52
已采纳

使用javascript函数获取html控件值

I have this table in my page that I fetch by using AJAX request.

<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>

So I'm trying to get the anchor value when the button is clicked. So I make a function like this :

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

But I'm not able to get the value. What is the correct way to fetch the value in the function?

  • 写回答

3条回答 默认 最新

  • douxin8610 2018-04-12 10:23
    关注

    Pass id direct to method:

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

    Then Yours js methods will be simpler:

    function deleteParticipantFunction(participant_id){
    var result = confirm('You are about to delete this participant. Continue?');
    if(result){
        //delete smth by id
    }
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码