每行带有按钮的 HTML 表格 [英] Html table with button on each row

查看:11
本文介绍了每行带有按钮的 HTML 表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多行一列的表格.每个表格单元格中都有一个按钮.像这样:

I have a table with multiple rows and one column. Each table cell has a button in it. Like this:

<table id="table1" border="1">
    <thead>
      <tr>
          <th>Select</th>
      </tr>
    </thead>
    <tbody>
       <tr> 
           <td>
               <form name="f2" action="javascript:select();" >
                <input id="edit" type="submit" name="edit" value="Edit" />
               </form>
           </td>
      </tr>
   </tbody>
</table>

我想要做什么:当按下其中一个按钮时,我想将其值从编辑"更改为修改".有什么想法吗?

What I want to do: when one of the buttons is pressed I would like to change its value from "Edit" to "Modify". Any idea?

推荐答案

很确定这可以解决您的问题:

Pretty sure this solves what you're looking for:

HTML:

<table>
    <tr><td><button class="editbtn">edit</button></td></tr>
    <tr><td><button class="editbtn">edit</button></td></tr>
    <tr><td><button class="editbtn">edit</button></td></tr>
    <tr><td><button class="editbtn">edit</button></td></tr>
</table>

Javascript(使用 jQuery):

$(document).ready(function(){
    $('.editbtn').click(function(){
        $(this).html($(this).html() == 'edit' ? 'modify' : 'edit');
    });
});

显然我应该先看看你的示例代码;)

Apparently I should have looked at your sample code first ;)

您需要更改(至少)每个元素的 ID 属性.ID 是页面上每个元素的唯一标识符,这意味着如果您有多个具有相同 ID 的项目,则会发生冲突.

You need to change (at least) the ID attribute of each element. The ID is the unique identifier for each element on the page, meaning that if you have multiple items with the same ID, you'll get conflicts.

通过使用类,您可以将相同的逻辑应用于多个元素而不会产生任何冲突.

By using classes, you can apply the same logic to multiple elements without any conflicts.

JSFiddle 示例

这篇关于每行带有按钮的 HTML 表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆