HTML - 使 td 可点击并在提交时传递值

标签 html css

我有以下工作示例:

http://jsfiddle.net/Jaron787/tw3x9xt7/2/

我想让每个 TDclickable(在它周围的边框上突出显示)和按钮 1 或 2 的 onclick 以传递被单击的 TD 的 ID。

最好的方法是什么? - 请更新工作示例

HTML

<div id="lse" class="display">
  <div id="centertbl">
    <table id="tblData" class="TSS">
      <tr>
        <td align="center" colspan="4"><b>Table 1</b></td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 1</td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 2</td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 3</td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 4</td>
      </tr>
    </table>

    <table id="tblData" class="TSS">
      <tr>
        <td align="center" colspan="4"><b>Table 2</b></td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 1</td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 2</td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 3</td>
      </tr>
      <tr>
        <td align="center" colspan="4">Data 4</td>
      </tr>
    </table>
  </div>

  <input type="submit" class="button button1" name="submitButton" value="Button 1">
  <input type="submit" class="button button1" name="submitButton" value="Button 2">
</div>

CSS

.TSS {
  border: 1px solid #000000;
  float: none;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 10.6px;
  font-style: normal;
  padding: 10px;
  text-decoration: none;
  display: inline-block;
}

#centertbl {
  text-align: center;
}

.button {
  background-color: #4CAF50;
  /* Green */
  border: none;
  color: white;
  padding: 5px 15px;
  text-align: center;
  text-decoration: none;
  display: block;
  font-size: 16px;
  margin: 4px 2px;
  -webkit-transition-duration: 0.4s;
  /* Safari */
  transition-duration: 0.4s;
  cursor: pointer;
}

.button1 {
  background-color: white;
  color: black;
  border: 2px solid #4CAF50;
}

.button1:hover {
  background-color: #4CAF50;
  color: white;
}

最佳答案

给每个td id:

<table id="tblData" class="TSS">
  <thead>
    <tr>
      <td align="center" colspan="4" id="td1"><b>Table 1</b></td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td align="center" colspan="4" id="td2">Data 1</td>
    </tr>
    <tr>
      <td align="center" colspan="4" id="td3">Data 2</td>
    </tr>
    <tr>
      <td align="center" colspan="4" id="td4">Data 3</td>
    </tr>
    <tr>
      <td align="center" colspan="4" id="td5">Data 4</td>
    </tr>
  </tbody>
</table>

<table id="tblData" class="TSS">
  <thead>
    <tr>
      <td align="center" colspan="4" id="td6"><b>Table 2</b></td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td align="center" colspan="4" id="td7">Data 1</td>
    </tr>
    <tr>
      <td align="center" colspan="4" id="td8">Data 2</td>
    </tr>
    <tr>
      <td align="center" colspan="4" id="td9">Data 3</td>
    </tr>
    <tr>
      <td align="center" colspan="4" id="td10">Data 4</td>
    </tr>
  </tbody>
</table>

还有 CSS 在你点击它们时突出显示 td:

.td-hover {
    background-color: #4CAF50;
}

假设您使用 jQuery,这里是 javascript:

//onlick of a td will highlight the td clicked, if it's already been   highlighted the onclick event will remove the highlight.
$("td").click(function() {   
    if ($(this).hasClass("td-hover")) {
        $(this).removeClass("td-hover");
    } else {
        $(this).addClass("td-hover");
    }
})

//click on the button 1 or 2 will capture all id of the td that is highlighted
$(".button").click(function() {
    $("td").each(function() {
        if ($(this).hasClass("td-hover")) {
        var id = $(this).attr("id");
        alert(id);  // replace your function here
        }
    })
})

关于HTML - 使 td 可点击并在提交时传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37046310/

相关文章:

html - 在不同宽度的图像上将 Div 定位在同一位置

jquery - 中心的 float div 在 IE 中不起作用

html - 无法弄清楚如何使用 CSS/HTML 制作包含多个图像的框

javascript - 我可以通过 Javascript 向表单添加参数吗?

html - 停止 css onhover 移动所有其他 block

html - 在图像幻灯片放映外添加边框

javascript - 水平 "paging"或在网页上滚动 "viewport"

css - Bootstrap 中的响应式正方形网格

html - 当元素从相对定位到固定但腾出的空间保持固定高度时,是否会发生页面回流

css - 调整移动按钮的大小