Mic*_*ser 2 jquery function-parameter show-hide
正如标题所示,我想将 show() 和 hide() jQuery 函数作为另一个函数中的参数传递。
function applyActionOnClass(attr, action) {
$('#salesonhold_table tr').each(function () {
if ($(this).attr('status') == attr)
$(this).action;
});
}
Run Code Online (Sandbox Code Playgroud)
这是我调用我的applyActionOnClass函数时的情况:
$(document).on('change', '.checkboxes', function () {
var attr = $(this).val();
if ($(this).is(':checked'))
applyActionOnClass(attr, show());
else
applyActionOnClass(attr, hide());
});
Run Code Online (Sandbox Code Playgroud)
为了稍微解释一下这样做的目的,我有一个表,每个表<tr>都有一个status彼此不同的属性。我在表格顶部有两个复选框,一个对应每个可能的status值,我想<tr>在触发复选框时隐藏/显示相应的值。
触发器和东西工作正常,但当谈到它时,$(this).action;它说hide is not defined。知道我在那里做错了什么吗?
太感谢了 !
另一个解决方案是像这样改变你的功能
function applyActionOnClass(attr, action) {
$('#salesonhold_table tr').each(function () {
if ($(this).attr('status') == attr)
$(this)[action]();
});
}
Run Code Online (Sandbox Code Playgroud)
注意$(this)[action]();你可以在这里看到它的工作http://jsfiddle.net/t6rEE/作为概念证明(我尝试了hide和show)
| 归档时间: |
|
| 查看次数: |
3823 次 |
| 最近记录: |