【问题标题】:passing 'this' as a parameter but operate in the context of the calling function将“this”作为参数传递,但在调用函数的上下文中操作
【发布时间】:2013-01-22 02:45:51
【问题描述】:

我希望能够卸载 setStatus() 以获取一些参数 - 但现在我什至无法让硬编码的选择器工作。如何正确卸载 setStatus() 函数?

HTML

<div id="first:test"> <span class="statusFlag"></span>
  <br/>
  <input type="checkbox" value="Stuff">Box 1</input>
  <input type="checkbox" value="Stuff">Box 2</input>
  <input type="checkbox" value="Stuff">Box 3</input>
</div>
<div id="second:test"> <span class="statusFlag"></span>
  <br/>
  <input type="checkbox" value="Stuff">Box 1</input>
  <input type="checkbox" value="Stuff">Box 2</input>
  <input type="checkbox" value="Stuff">Box 3</input>
</div>
<div id="noTest">
  <input type="checkbox" value="stuff">No Validate</input>
</div>

jQuery

$('[id$=test] input:checkbox').change(function () {
  if ($(this).parent().children('input:checkbox').filter(':checked').length > 0) {
    setStatus(this);
  } else {
    $(this).closest('[id$=test]').find('.statusFlag').css('color', 'red').text('Incomplete');
  }
});

function setStatus(oldthis){
  alert(oldthis);
  $(oldthis).closest('[id$=test]').find('statusFlag').css('color', 'green').text('Complete');
}

这个想法是动态传递选择器并为每个 div 部分设置文本......所以最终类似于:

setStatus(this, '[id$=test]', 'red', 'Incomplete');

在这里提琴 http://jsfiddle.net/pMAXs/

【问题讨论】:

    标签: jquery jquery-selectors parameter-passing this


    【解决方案1】:

    您没有在第二个选择器中找到类。

    $(oldthis).closest('[id$=test]').find('statusFlag').css('color', 'green').text('Complete');
    

    "statusFlag" -> ".statusFlag"

    假设我理解您的最终目标是正确的,请参阅:http://jsfiddle.net/pMAXs/3/

    【讨论】:

    • 呃。感觉就像星期一。感谢另一双眼睛的帮助。
    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多