简体   繁体   English

javascript函数在document.ready()下无法正常工作

[英]javascript functions not working properly under document.ready()

When ever I try adding the second function, the JavaScript ceases to work on the page. 每当我尝试添加第二个函数时,JavaScript就会在页面上停止工作。 Can you guys help with possible error it is? 你们可以帮忙解决可能的错误吗? Thanks. 谢谢。

Note: If I comment out the second function, it works fine and the PHP code is an element from CaKePHP. 注意:如果我注释掉第二个函数,它可以正常工作,并且PHP代码是CaKePHP的元素。

<script type="text/javascript">
    $(document).ready(function() {
        $("#is_sublet").click(function() {
            $("#sublet_dates").slideToggle();
            return false;
        });
    });

    $(document).ready(function() {
        $("#custom_rates").click(function() { 
            $(".avi_specialrates").append($('<?php echo $this->element('custom_price_per_night', array('config' => 'sec')); ?>');
            return false;
        }); 
    }); 
</script>

This line: 这行:

$(".avi_specialrates").append($('<?php echo $this->element('custom_price_per_night', array('config' => 'sec')); ?>');

Requires another closing parenthesis at the end: 最后需要另一个右括号:

$(".avi_specialrates").append($('<?php echo $this->element('custom_price_per_night', array('config' => 'sec')); ?>'));

If you remove the PHP it's easier to see: 如果删除PHP,则更容易看到:

$(".avi_specialrates").append($('<PHP WENT HERE>')/*right here you need a `)`*/;

Having errors like this will stop the JavaScript on the page from functioning properly. 出现此类错误将阻止页面上的JavaScript正常运行。

I think you have a syntax error. 我认为您有语法错误。 Change: 更改:

 $(".avi_specialrates").append($('<?php echo $this->element('custom_price_per_night', array('config' => 'sec')); ?>');

by 通过

 $(".avi_specialrates").append($('<?php echo $this->element('custom_price_per_night', array('config' => 'sec')); ?>'));

One parentheses is missing 缺少一个括号

This should work. 这应该工作。

<script type="text/javascript">
$(document).ready(function() {
   $("#is_sublet").click(function() {
   $("#sublet_dates").slideToggle();
   return false;
   });

    $("#custom_rates").click(function() { 
    $(".avi_specialrates").append($('<?php echo $this->element('custom_price_per_night', array('config' => 'sec')); ?>'));
    return false;
    });
}); 
</script>

Instead of declaring (document).ready twice, try doing it like this: 与其声明(document).ready两次,不如尝试这样做:

<script type="text/javascript">
    $(document).ready(function(){
          $("#is_sublet").click(function()
          {
              $("#sublet_dates").slideToggle();
              return false;
    });

    $("#custom_rates").click(function()
    { 
        $(".avi_specialrates").append($('<?php echo $this->element('custom_price_per_night', array('config' => 'sec')); ?>');
        return false;
    }); 
}); 
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM