jQuery:根据另一个复选框

时间:2016-03-25 07:36:47

标签: javascript jquery html

我有以下情况。当我点击表格中的值时,我试图将选择框的值设置为点击的值。但是只应设置选中复选框的选择框。

我尝试使用:eq,但无法弄清楚确切的方法。有没有办法实现这个目标?

这是我的示例代码。

(select和checkbox列之间可能还有其他列,这是一个更大问题的一部分,我创建的是样本)



var selectValues = ["-","somevalue1", "somevalue2", "somevalue3", "somevalue4", "somevalue5", "somevalue6", "somevalue7", "somevalue8", "somevalue9", "somevalue10"];

for (var i=0;i<selectValues.length;i++){
   $('<option/>').val(i).html(selectValues[i]).appendTo('.selectbox');
}


$("#sample td").click(function() {
  var clickedValue = $(this).text();
  console.log(clickedValue);
  
  $('.selectbox').val(clickedValue);
});
&#13;
table {
  border: 1px solid #ccc;
  border-collapse: collapse;
}
#sample tr {
  border: 1px solid #ccc;
}
td {
  cursor: pointer;
  border: 1px solid #ccc;
  padding: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="sample">
  <table>
    <thead>
      <tr>
        <td>something</td>
        <td>something else</td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>somevalue1</td>
        <td>somevalue2</td>
      </tr>
      <tr>
        <td>somevalue3</td>
        <td>somevalue4</td>
      </tr>
      <tr>
        <td>somevalue5</td>
        <td>somevalue6</td>
      </tr>
      <tr>
        <td>somevalue7</td>
        <td>somevalue8</td>
      </tr>
      <tr>
        <td>somevalue9</td>
        <td>somevalue10</td>
      </tr>

    </tbody>
  </table>
</div>


<table>
  <tr>
    <td>
      <input type="checkbox" class="checkbox">
    </td>
    <td>
      <select class="selectbox"></select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkbox">
    </td>
    <td>
      <select class="selectbox"></select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkbox">
    </td>
    <td>
      <select class="selectbox"></select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkbox">
    </td>
    <td>
      <select class="selectbox"></select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkbox">
    </td>
    <td>
      <select class="selectbox"></select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkbox">
    </td>
    <td>
      <select class="selectbox"></select>
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
var selectValues = ["-", "somevalue1", "somevalue2", "somevalue3", "somevalue4", "somevalue5", "somevalue6", "somevalue7", "somevalue8", "somevalue9", "somevalue10"];

$.each(selectValues, function (i, val) {
  $('<option/>', {value: val, text: val}).appendTo('.selectbox');
});

$("#sample").on("click", "td", function() {
  var clickedValue = $(this).text();
  $("#target :checkbox:checked").closest("tr").find(".selectbox").val(clickedValue);
});
&#13;
table {
  border: 1px solid #ccc;
  border-collapse: collapse;
  float: left;
}
#sample tr {
  border: 1px solid #ccc;
}
td {
  cursor: pointer;
  border: 1px solid #ccc;
  padding: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="sample">
  <table>
    <thead>
      <tr><td>something</td><td>something else</td></tr>
    </thead>
    <tbody>
      <tr><td>somevalue1</td><td>somevalue2</td></tr>
      <tr><td>somevalue3</td><td>somevalue4</td></tr>
      <tr><td>somevalue5</td><td>somevalue6</td></tr>
      <tr><td>somevalue7</td><td>somevalue8</td></tr>
      <tr><td>somevalue9</td><td>somevalue10</td></tr>
    </tbody>
  </table>
</div>

<table id="target">
  <tr>
    <td><input type="checkbox" class="checkbox"></td>
    <td><select class="selectbox"></select></td>
  </tr>
  <tr>
    <td><input type="checkbox" class="checkbox"></td>
    <td><select class="selectbox"></select></td>
  </tr>
  <tr>
    <td><input type="checkbox" class="checkbox"></td>
    <td><select class="selectbox"></select></td>
  </tr>
  <tr>
    <td><input type="checkbox" class="checkbox"></td>
    <td><select class="selectbox"></select></td>
  </tr>
  <tr>
    <td><input type="checkbox" class="checkbox"></td>
    <td><select class="selectbox"></select></td>
  </tr>
  <tr>
    <td><input type="checkbox" class="checkbox"></td>
    <td><select class="selectbox"></select></td>
  </tr>
  <tr>
    <td><input type="checkbox" class="checkbox"></td>
    <td><select class="selectbox"></select></td>
  </tr>
</table>
&#13;
&#13;
&#13;