2在一个页面上的表 - 行上的按钮运行不同的脚本

时间:2017-10-12 02:19:49

标签: javascript jquery html

我有一个包含2个表的页面,每行都有一个'选择'单击时需要运行脚本的按钮。我已经使用了第一个表,但无法解决如何使用这两个表。

这里是表格的HTML:



<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<h2>Select Main</h2>

<div>

  <br />
  <table class="table table-condensed table-striped table-bordered">
    <thead>
      <th scope="col">Code</th>
      <th scope="col">Description</th>
      <th class="text-center" scope="col">Select</th>
    </thead>
    <tbody>

      <tr class="" id="SK5543333">
        <td>BJ2345</td>
        <td>Laptop 13 inch display</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr class="" id="SK3241235213">
        <td>AZ77656</td>
        <td>Laptop 15 inch display</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>


    </tbody>
  </table>

</div>


<h2>Select Accessories</h2>

<div>

  <br />
  <table class="table table-condensed table-striped table-bordered">
    <thead>
      <th scope="col">Code</th>
      <th scope="col">Description</th>
      <th class="text-center" scope="col">Select</th>
    </thead>
    <tbody>

      <tr class="" id="SK3412541">
        <td>MM42412341</td>
        <td>Mouse</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr class="" id="SK95390485">
        <td>KB42341243</td>
        <td>Keyboard</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr class="" id="SK42353">
        <td>USB421341234</td>
        <td>USB Adapter</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr class="" id="SK543647585">
        <td>PWR363456534</td>
        <td>POWER ADAPTER</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
    </tbody>
  </table>

</div>
&#13;
&#13;
&#13;

当用户从top / Main表中选择时,我需要运行不同的脚本,当用户从bottom / Accessories表中选择时,我需要运行另一个脚本。这是我点击第一个表格中的选择按钮时运行的脚本:

&#13;
&#13;
$(document).ready(function() {
  $('button.btn-success:not([type="submit"])').click(function() {
    // Remove the classes from all of the TR elements
    $(this).parents('table').find('tr').removeClass('success warning danger');
    var productID = $(this).closest('tr').attr('id');
    console.log(productID);
    $this = $(this);
    // add the success class to the row and remove the danger class if it was present
    $this.closest('tr').addClass("success");
    $this.closest('tr').removeClass("danger");
    // update the hidden input with the selected productID
    $('#productID').val(productID);
  });
});
&#13;
&#13;
&#13;

不确定如何使用仅在点击第二个表格中的“选择”按钮时运行的另一个版本?

2 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西:

$(document).ready(function() {
  $("table:first").find('button[type!="submit"]').click(function() {
    $("table:first").find("tr").removeClass("success warning danger");
    $(this).parents("tr").addClass("success");
    $("#productID").val($(this).parents("tr").attr("id"));
  });
  $("table").eq(1).find('button[type!="submit"]').click(function() {
    $("table").eq(1).find("tr").removeClass("success warning danger");
    //Do whatever color you want
    $(this).parents("tr").addClass("warning");
    //Wasn't provided this snippet so I just made up accessoryID
    $("#accessoryID").val($(this).parents("tr").attr("id"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<h2>Select Main</h2>
<div>
  <br />
  <table class="table table-condensed table-striped table-bordered">
    <thead>
      <tr>
        <th>Code</th>
        <th>Description</th>
        <th class="text-center">Select</th>
      </tr>
    </thead>
    <tbody>
      <tr id="SK5543333">
        <td>BJ2345</td>
        <td>Laptop 13 inch display</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr id="SK3241235213">
        <td>AZ77656</td>
        <td>Laptop 15 inch display</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
    </tbody>
  </table>
</div>
<h2>Select Accessories</h2>
<div>
  <br />
  <table class="table table-condensed table-striped table-bordered">
    <thead>
      <tr>
        <th>Code</th>
        <th>Description</th>
        <th class="text-center">Select</th>
      </tr>
    </thead>
    <tbody>
      <tr id="SK3412541">
        <td>MM42412341</td>
        <td>Mouse</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr id="SK95390485">
        <td>KB42341243</td>
        <td>Keyboard</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr id="SK42353">
        <td>USB421341234</td>
        <td>USB Adapter</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
      <tr id="SK543647585">
        <td>PWR363456534</td>
        <td>POWER ADAPTER</td>
        <td class="text-center"><button type="button" class="btn btn-success btn-sm">Select</button></td>
      </tr>
    </tbody>
  </table>
</div>

.eq这将通过索引从jQuery对象列表中返回jQuery对象,而使用括号进行索引将返回HTML内容。

P.S。您在表格中确实不需要class=""scope="col",我提供的HTML没有这些。 class属性的唯一真正要点是轻松分配多个CSS属性,除非您使用类作为选择器。与范围相同,我制作了许多表,从未使用过范围。

此外,当您执行thead时,您需要在添加标头之前添加tr。我修复了上面的代码。 (是的,这确实意味着表支持多个标题行)。

答案 1 :(得分:0)

中断了写答案,但你可以尝试这样的事情:

const selectHelper =
  ($me,classToAdd,inputSelector) => {
    $me.parents("table")
      .eq(0)
      .find("tr")
      .removeClass("success warning danger");
    const id = 
      $me.parents("tr")
        .eq(0)
        .addClass(classToAdd)
        .attr("id")
    ;
    $(inputSelector).val(id);
  }
;
const clickActions = {
  one:(e,$me)=>
    selectHelper(
      $me
      ,"warning"
      ,"#inputidOne"
    )
  ,two:(e,$me)=>
    selectHelper(
      $me
      ,"success"
      ,"#inputidTwo"
    )
};
$(document.body)
  .on(
    "click"
    ,`[data-action-click]`
    ,e=>{
      const action = e.target.getAttribute("data-action-click");
      if(typeof clickActions[action] === "function"){
        clickActions[action](e,$(e.target))
      }
      debugger;
    }
  )

该按钮需要一个data-action-click属性,该属性具有您在单击时要执行的功能的名称:

  <button 
    type="button"
    data-action-click="two"
    class="btn btn-success btn-sm"
  >
    Select
  </button>