$.fn.show=function(){
alert($(this).val());
};  $("#show").click(function(){$.fn.show();});
<input type="button" value="showval" id="show">这几行代码, 我是想点击button之后弹出showval这个字符串的。但是现在的结果是undefined
求助

解决方案 »

  1.   

    $.fn.show=function(node){
    alert($(node).val());
    };  $("#show").click(function({$.fn.show($("this"));});
      

  2.   

    <input type="button" value="showval" id="show">
    <script type="text/javascript">
    $("#show").click(function(){
    alert($(this).val());
    });
    </script>
      

  3.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <title>无标题文档</title>
    <script type="text/javascript">
    (function($){
    $.fn.showBox=function(){
    //alert(this.attr('value'));
    return this.attr('value','#f00');
    }
    })(jQuery);jQuery(function($){
    $("#show").click(function(){
    $(this).showBox();
    });
    });
    </script>
    </head><body>
    <input type="button" value="showval" id="show" />
    </body>
    </html>
      

  4.   

    $.函数名 和 $.fn.函数名是有区别的
    <script type="text/javascript">
    (function($){
    $.fn.showBox=function(){
    //alert(this.attr('value'));
    return this.attr('value','#f00');
    }
    $.alertBox=function(ele){
    alert($(ele).attr('value'));
    }
    })(jQuery);jQuery(function($){
    $("#show").click(function(){
    //$(this).showBox();
    $.alertBox(this);
    });
    });
    </script>
      

  5.   

    你的例子中,show方法的添加和click函数的绑定都是有问题的。
    修改后如下://$.fn.show是给每个jquery对象添加了一个show方法以供其调用,所以在这个函数的具体执行中
    //this指代的已经是jquery对象无需再加“$”
    $.fn.show=function(){
    alert(this.val());
    };//click函数中的this指的是input元素而非jquery对象,所以需要加“$”转化为jquery对象来调用show方法
    $("#show").click(
    function(){
    $(this).show()
    }
    );
      

  6.   

    楼主写的代码里面 this应该指向的是window全局对象 而非input元素 所以才会出现undefined,怎么解决楼上的都写了