javascript Jquery:不推荐使用属性的指定属性。它总是返回真

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12352171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:59:56  来源:igfitidea点击:

Jquery: Use of attributes' specified attribute is deprecated. It always returns true

javascriptjqueryonchange

提问by Alex

I have collection of select elements, when I change value of one select elements for first time this function dont work it works the second time I change value and in console I have this

我有选择元素的集合,当我第一次更改一个选择元素的值时,此功能不起作用,第二次我更改值时它起作用,在控制台中我有 这

jQuery code:

jQuery代码:

$(document).ready(function () {
    var collection = $('select.ddlJ');
    console.log(collection);
    for (var element in collection) {
        $(element).change(function () {

            $('select.ddlJ').change(function (e) {
                $(this).parent().parent().find('td:last').prev().find('span').html(
                    $(this).parent().parent().find('select.ddlJ').filter(function () {
                        return $.trim($(this).val()) == 'm';
                    }).length);

                $(this).parent().parent().find('td:last span').html(
                $(this).parent().parent().find('select.ddlJ').filter(function () {
                    return $.trim($(this).val()) == 'n'; }).length);
            });
        });
    }
});

HTML code:

HTML代码:

<table cellspacing="0" border="1" style="border-collapse:collapse;" id="grid1"
   rules="all">
        <tbody><tr>
 <th scope="col">Nr de ord</th>
 <th scope="col">StudentId</th>
 <th scope="col">Name LName</th>

 <th scope="col">
      <span id="mondayText">monday<br></span>
      <span id="monday">14.05.2012</span>
  </th>
<th scope="col">
     <span id="thuesdayText">thuesday<br></span>
     <span id="thuesday">15.05.2012</span>
</th>
<th scope="col">
     <span id="wednesdayText">wednesday<br></span>
     <span id="wednesday">16.05.2012</span>
</th>
<th scope="col">
     <span id="thursdayText">thursday<br></span>
     <span id="thursday">17.05.2012</span>
</th>
<th scope="col">
     <span id="fridayText">friday<br></span>
     <span id="friday">18.05.2012</span>
</th>
<th scope="col">
     <span id="saturdayText">saturday<br></span>
     <span id="saturday">19.05.2012</span>
  </th>
   <th scope="col">
   <span id="M">Total1</span>
   </th>
  <th scope="col">
   <span id="N">Total2</span>
   </th>
               </tr><tr>
                <td>  1  </td>
                <td>110001</td>
                <td>Test1 Test1</td><td>
                </td><td>
                </td><td>
                </td><td>
 <select class="ddlJ" id="a1111_0" name="ctl00$contentbody$grid1$ctl02$a1111">
                <option value="a" selected="selected">a</option>
                <option value="m">m</option>
                <option value="n">n</option>
            </select>
 <select class="ddlJ" id="a2222_0" name="ctl00$contentbody$grid1$ctl02$a2222">
                <option value="a" selected="selected">a</option>
                <option value="m">m</option>
                <option value="n">n</option>
            </select>

                          </td><td>
                          </td><td>
                          </td><td>
                          <span class="label" id="totalM"></span>
                          </td><td>
                           <span id="totalN"></span>
                          </td>
        </tr><tr>
        <td> 2   </td>
                <td>110002</td>
                <td>Test2 Test2</td>
                <td></td>
               <td></td>
               <td></td><td>
 <select class="ddlJ" id="a1111_1" name="ctl00$contentbody$grid1$ctl03$a1111">
                <option value="a" selected="selected">a</option>
                <option value="m">m</option>
                <option value="n">n</option>
            </select>

                </td><td>
                </td><td>
                </td><td>
                <span class="label" id="totalM"></span>
                 </td><td>
                 <span id="totalN"></span>
                  </td>
        </tr><tr>
            <td>
                         3                  
                </td><td>110008</td><td>Test3 Test3</td><td>
                </td><td>
                </td><td>
                </td><td>
<select class="ddlJ" id="a1111_2" name="ctl00$contentbody$grid1$ctl04$a1111">
                <option value="a" selected="selected">a</option>
                <option value="m">m</option>
                <option value="n">n</option>
            </select>
                         </td><td>
                </td><td>
                </td><td>
                 <span class="label" id="totalM"></span>
                </td><td>
                  <span id="totalN"></span>
                          </td>
        </tr>
    </tbody>
</table>

回答by Alessio Gaeta

回答by iX3

Try changing

尝试改变

var collection = $('select.ddlJ')

to use jQuery.get()

使用jQuery.get()

var collection = $('select.ddlJ').get();    
// BTW, this is similar to $('select.ddlJ')[0] 

That gives you the actual DOM elements rather than a jQuery object matching them. Alternatively (and the way I would probably do it), would be to instead change

这为您提供了实际的 DOM 元素,而不是匹配它们的 jQuery 对象。或者(以及我可能会这样做的方式),而是改变

for (var element in collection)

to use jQuery.each()

使用jQuery.each()

$('select.ddlJ').each(function(i, element) { ... });

回答by DRC

Try changing the .val() to .attr('value')

尝试将 .val() 更改为 .attr('value')