如何使用ajax交换两个表格单元格值并更新到数据库中

问题描述 投票:2回答:2

javascript代码

$("#table td").click(function () {

    $(this).addClass('selected').siblings().removeClass('selected');
    var value = $(this).find('td').html();
   // alert(value);
});

$('.swapbutton').on('click', function (e) {
    alert($("#table td.selected td:first").html());
});

CSS

  td {
    border: 1px #DDD solid;
    padding: 5px;
    cursor: pointer;
   }

   .selected {
    background-color: brown;
    color: #FFF;
   }

enter image description here

请看上面的图片

  • 我想要交换两个表格单元格值
  • 我想在单击“交换”按钮并交换它时获得所选两个单元格的值

如何使用JavaScript或Jquery也可以更新到数据库中

javascript jquery html css jquery-ui
2个回答
2
投票

这是一个简单的解决方案,我认为你想做什么:

如果你需要更多限制它的工作方式让我知道,我很乐意调整。但这应该是一个很好的起点。

$(function() {
  $("table td:not(.notSelectable)").click(function() {
    $(".lastSelected").removeClass("lastSelected");
    $(".selected").toggleClass("selected lastSelected");
    $(this).addClass("selected");
  });
});

function swap() {
  // We cannot swap if there are not 2 selected items
  if ($(".selected, .lastSelected").length != 2) { return; }

  // Set label with date data
  $("#lblSelectedDate").text($(".selected").siblings(".date").text());
  $("#lblLastSelectedDate").text($(".lastSelected").siblings(".date").text());

  // Set label with value data
  $("#lblSelectedValue").text($(".selected").children("input[type=hidden]").val());
  $("#lblLastSelectedValue").text($(".lastSelected").children("input[type=hidden]").val());

  // Swap cell data
  var temp = $(".lastSelected").html();
  $(".lastSelected").html($(".selected").html());
  $(".selected").html(temp);
  $(".selected, .lastSelected").removeClass();
}
td:not(.notSelectable) {
  cursor: pointer
}

td.selected,
td.lastSelected {
  background-color: maroon;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button onclick="swap();">Swap</button>
<br /><br />
Selected Date: <span id="lblSelectedDate"></span>
<br /> Selected Value: <span id="lblSelectedValue"></span>
<br /> Last Selected Date: <span id="lblLastSelectedDate"></span>
<br /> Last Selected Value: <span id="lblLastSelectedValue"></span>
<br /><br />

<table border="solid">
  <thead>
    <th>Date</th><th>First</th><th>Second</th><th>Third</th>
  </thead>
  <tbody>
    <tr>
      <td class="notSelectable date">01/01/2018 10:00 pm</td>
      <td>00<input type="hidden" value="00" /></td>
      <td>01<input type="hidden" value="01" /></td>
      <td>02<input type="hidden" value="02" /></td>
    </tr>
    <tr>
      <td class="notSelectable date">01/01/2018 11:00 pm</td>
      <td>10<input type="hidden" value="10" /></td>
      <td>11<input type="hidden" value="11" /></td>
      <td>12<input type="hidden" value="12" /></td>
    </tr>
    <tr>
      <td class="notSelectable date">01/01/2018 12:00 pm</td>
      <td>20<input type="hidden" value="20" /></td>
      <td>21<input type="hidden" value="21" /></td>
      <td>22<input type="hidden" value="22" /></td>
    </tr>
  </tbody>
</table>

0
投票
var value = [];
var tdObj = [];
$("#table td").click(function () {
    $(this).addClass('selected').siblings().removeClass('selected');
    for(var i=0; i<2;i++) {
        tdObj[i] = $(this).find('td');
        value[i] = tdObj.text();
        if(i>1) return false;
    }
    // alert(value);
});

$('.swapbutton').on('click', function (e) {
    for(var i=2; i>0;i--) {
        value[i] = tdObj[i].text();
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.