【发布时间】:2011-05-27 03:16:20
【问题描述】:
我希望这是一个相当基本的问题。在下面的 javascript 中,我想运行 checkval() 函数,就好像它是在“this”(在 checkval 内)指的是插件被调用的元素(嗯,元素之一)的范围内执行的)。
所以如果我打电话
$('.has_hidden').conditional();
然后将对所有这些项目运行 checkval 并将其绑定到 on change 事件。
原因是,我有一个表单,如果您输入一些内容,它将取消隐藏更多选项,例如,如果您输入“australia”,它将取消隐藏一堆其他元素。现在,当我保存并重新加载该表单时,我的 php 脚本会重新填充数据,但其他元素仍然隐藏。我只需要运行 check val 以便它们不被隐藏。明白马
/**
* This class will show or hide a bunch of elements (A,B,C) based on the value in element (E, this).
* To use it, take the name of the form element (E, this) and apply it as a class to A,B,C
* Then add a display none to A,B,C (add class off)
* Set the "rel" tag on
* Finally, run $(E).conditional();
*/
(function( $ ){
$.fn.conditional = function() {
return this.each(function(){
var $this = $(this);
$this.unbind('change.cns').bind('change.cns', checkval);
});
function checkval(){
var regex = $(this).attr('rel');
var class_name =$(this).attr('name');
if($(this).val().search(regex)>-1)
$('.'+class_name).show();
else
$('.'+class_name).hide();
}
};
})( jQuery );
【问题讨论】:
标签: jquery jquery-plugins scope