【问题标题】:Uncaught TypeError: $(...).live is not a function未捕获的类型错误:$(...).live 不是函数
【发布时间】:2020-02-11 17:04:48
【问题描述】:

我正在使用 jquery 1.10.2 并且遇到 Uncaught TypeError: $(...).live is not a function。我正在使用 Codeigniter 进行 pyrocms 的开发。下面我写了代码:-

public static function close()
    {
        return '</form><script>$(document).ready(function(){$("a.close").live("click", function(e){ e.preventDefault();$(this).fadeTo(200, 0);$(this).parent().fadeTo(200, 0);$(this).parent().slideUp(400, function(){$(this).remove()})})});</script>';
    }

我是新手。任何帮助将不胜感激。提前致谢。

编辑:-

从下面的函数中,我调用了 close 函数。请检查以下代码:-

public static function form($form_name, $output_array=false)
    {

        if ($output_array)
        {
            $return            = self::get_fields_arrays($form_name);
            $return['header']  = self::open($form_name) . $return['header'];
            $return['footer'] .= self::close();
        } else {
            $return  = self::open($form_name);
            $return .= self::get_fields($form_name);
            $return .= self::close();
        }

        self::$_ci->load->library('jquery_validation');
        if ($valid = self::get_validation_array($form_name))
        {
            self::$_ci->jquery_validation->set_rules($valid);
            if (is_array($return))
            {
                $return['footer'] .= self::$_ci->jquery_validation->run('#frm-'.$form_name);
            }
            else
            {
                $return .= self::$_ci->jquery_validation->run('#frm-'.$form_name);
            }
        }

        return $return;
    }

从这个标签开始调用它。

<li class="single current parent"><a href="http://php-pc/adina225.dev/hunter-valley-wine-club/wine-club-application">Wine Club Application</a></li>

Jogi Mehul 如果您需要更多帮助,请告诉我。

我已经按照 Jogi 的建议编辑了关闭功能。所以它看起来像下面:-

public static function close()
    {
        return '</form><script>$(document).ready(function(){$("a.close").on("click", function(e){ e.preventDefault();$(this).fadeTo(200, 0);$(this).parent().fadeTo(200, 0);$(this).parent().slideUp(400, function(){$(this).remove()})})});</script>';
    }

现在我遇到以下错误:- Uncaught ReferenceError: pyroforms is not defined

private function build_script($form_name) 
    {
        $script = '<script>$(document).ready(function() { pyroforms.init($("'.$form_name.'"), {submitHandler: function(form) { pyroforms.submit($("'.$form_name.'"), form)}, rules: %s,messages: %s});})</script>';
        return sprintf($script, $this->rules, ($this->messages ? $this->messages : '{}'));
    }

【问题讨论】:

  • 我不知道你的要求是什么。为什么你在控制器而不是视图中编写 jquery?。
  • 好吧,我正在使用 pyrocms,所以我必须这样做。

标签: jquery oop codeigniter-3 pyrocms


【解决方案1】:

live() 方法在 jQuery 1.7 版中被弃用,并在 1.9 版中被删除。请改用 on() 方法。请参考https://www.w3schools.com/jquery/event_live.asp

public static function close()
    {
        return '</form><script>$(document).ready(function(){$("a.close").on("click", function(e){ e.preventDefault();$(this).fadeTo(200, 0);$(this).parent().fadeTo(200, 0);$(this).parent().slideUp(400, function(){$(this).remove()})})});</script>';
    }

【讨论】:

  • 我知道我无法修改此代码,所以我需要一些帮助。
  • @AlekStephanok 我已经更新了答案,请在 sn-p 上方尝试。
  • @AlekStephanok 你能告诉我你什么时候调用这个函数吗?
  • 在用你的 sn-p 更改我的 sn-p 后,我遇到 Uncaught ReferenceError: pyroforms is not defined 错误。
  • 检查你的参考你把jquery放在哪里,如果它不在你页面的head部分,那么把它移到head部分,因为jQuery需要首先加载,并且在如果要在$(document).ready( function(){}) 中编写jQuery 代码,请先加载。
【解决方案2】:

所以下面的代码解决了我的问题。

/**
    * Build jQuery validationcode
    *
    * @access private
    * @param  string
    * @return string
    */
    private function build_script($form_name) 
    {
        $script = '<script>$(document).ready(function() { function pyroforms(){} pyroforms.init = function(){}; new pyroforms(); pyroforms.init($("'.$form_name.'"), {submitHandler: function(form) { pyroforms.submit($("'.$form_name.'"), form)}, rules: %s,messages: %s});})</script>';
        return sprintf($script, $this->rules, ($this->messages ? $this->messages : '{}'));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2019-06-06
    • 2019-05-24
    • 2021-12-15
    • 2019-10-26
    • 2016-06-27
    相关资源
    最近更新 更多