简体   繁体   English

为jquery中所有选中的行设置“选择”选项?

[英]Set “Select” option for all checked rows in jquery?

Still learning jquery here, so forgive any ignorance. 还在这里学习jQuery,因此请原谅任何无知。

I have a table that contains a number of rows. 我有一个包含许多行的表。 Each row has a <select> tag and I want to "toggle" its value when I execute a function. 每行都有一个<select>标记,我想在执行函数时“切换”其值。 So here's what the table looks like for example: 因此,例如,表的外观如下:

<table align="center" class="table table-bordered table-hover table-condensed table-responsive">
  <thead>
    ...
  </thead>
  <tbody>
    <tr>
      <td class="text-center"><input id="item_ids_" name="item_ids[]" type="checkbox" value="11521"></td>
      <td class="text-center">Item value here</td>
      <td class="text-center">
        <select class="form-control" id="item_11521_color" name="item[11521][color]">
          <option selected="selected" value="None">None</option>
          <option value="Blue">Blue</option>
          <option value="Orange">Orange</option>
          <option value="Green">Green</option>
          <option value="Yellow">Yellow</option>
          <option value="Purple">Purple</option>
        </select>
      </td>
      <td class="text-center">
        <select class="form-control" id="item_11521_test" name="item[11521][test]">
          <option selected="selected" value="None">None</option>
          <option value="First">First</option>
          <option value="Second">Second</option>
        </select>
      </td>
    </tr>
  </tbody>
</table>

How can I iterate through each row and change the value (and text) <option value="Blue"> to <option value="Orange"> etc each time I run this function? 每次运行此函数时,如何遍历每一行并将值(和文本) <option value="Blue">更改为<option value="Orange">等?

Is this what you are looking for? 这是你想要的? (code updated) (代码已更新)

$("#change").on("click", function() {

    // loop throw all rows
    $("table tr").each(function() {

        // on a row, find the checkbox and change the values if it's checked
        if($(this).find("input[type='checkbox']").prop("checked")) {
            $(this).find(".color-select").val("Green");
            $(this).find(".position-select").val("First");
        } 

    });

});

Added a button to perform the change: 添加了一个按钮来执行更改:

<button id="change">change all to Green and first</button>

And classes to the selects for simplicity: 为了简单起见,选择的类:

<select class="form-control color-select" id="item_11521_color" name="item[11521 [color]">
<select class="form-control position-select" id="item_11521_test" name="item[11521][test]">

EDIT: 编辑:

Made required changes to the code. 对代码进行必要的更改。

The updated fiddle: http://jsfiddle.net/theagitator/44vqugf0/1/ 更新的小提琴: http : //jsfiddle.net/theagitator/44vqugf0/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM