简体   繁体   English

延迟更改动作jQuery

[英]Delay in change of action jQuery

I am working on a project in Symfony. 我正在Symfony中从事一个项目。 I have a table with some rows, and the end of each row there are four options. 我有一个带有一些行的表,每行的末尾有四个选项。 Based on the option chosen in each row, I want the color of the entire row to change. 根据每行中选择的选项,我希望更改整个行的颜色。 Now, this code works fine on jsfiddle, but however in my project, for each row, when I choose some option for the first time, there is no effect, the color does not change. 现在,此代码可以在jsfiddle上正常工作,但是在我的项目中,对于每一行,当我第一次选择某个选项时,都没有效果,颜色也不会改变。 When I choose some option the second time, the color changes. 当我第二次选择某个选项时,颜色会改变。 This is applicable for all rows. 这适用于所有行。 I want the color to change the first time itself like it is happening in jsfiddle. 我希望颜色本身会像jsfiddle中一样发生第一次更改。 Link here: https://jsfiddle.net/wqeob4jo/2/ 链接在这里: https : //jsfiddle.net/wqeob4jo/2/

PS: I am using REACT JS PS:我正在使用REACT JS

 $(document).ready(function() { $('select').change(function() { var selectedVal = this.value; if (selectedVal == "2") { $(this).closest('tr').removeClass().addClass('not_wanted') } else if (selectedVal == "3") { $(this).closest('tr').removeClass().addClass('not_available') } else if (selectedVal == "4") { $(this).closest('tr').removeClass().addClass('vacation') } else if (selectedVal == "5") { $(this).closest('tr').removeClass().addClass('priority_off') } }); }); 
 table td, th { border: 1px solid black; padding-left: 100px; } th { color: black; } tr.not_wanted { color: red; } tr.not_available { color: blue; } tr.vacation { color: orange; } tr.priority_off { color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table"> <tr style="background-color:grey;"> <th>Col 1</th> <th>Col 2</th> <th>Col 3</th> </tr> <tr class="options"> <td>Date 1</td> <td>Date 2</td> <td> <select id="select"> <option value="2">not_wanted</option> <option value="3">not_available</option> <option value="4">vacation</option> <option value="5">priority_off</option> </select> </td> </tr> <tr class="options"> <td>Date 1</td> <td>Date 2</td> <td> <select id="select"> <option value="2">not_wanted</option> <option value="3">not_available</option> <option value="4">vacation</option> <option value="5">priority_off</option> </select> </td> </tr> </table> 

I fixed this issue by using 我通过使用解决了这个问题

$(document).on('change','#select',function () {...

instead of 代替

$('select').change(function() {...

full function: 功能齐全:

$(document).on('change','#select',function () {
    var selectedVal = this.value;
    if (selectedVal == "2") {
      $(this).closest('tr').removeClass().addClass('not_wanted');
    } else if (selectedVal == "3") {
      $(this).closest('tr').removeClass().addClass('not_available');
    } else if (selectedVal == "4") {
      $(this).closest('tr').removeClass().addClass('vacation');
    } else if (selectedVal == "5") {
      $(this).closest('tr').removeClass().addClass('priority_off');
    }
  });

Hope it helps! 希望能帮助到你!

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

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