Las funciones de JavaScript no funcionan correctamente en document.ready ()

When ever I try adding the second function, the JavaScript ceases to work on the page. 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.

<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>

preguntado el 08 de noviembre de 11 a las 19:11

What is the rendered code? (I.e. the output received by the browser.) -

Choose an option: A] Your PHP is not parsed. B] The PHP added unescaped single-quote character and/or newline character(s). -

4 Respuestas

Esta línea:

$(".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:

$(".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.

respondido 08 nov., 11:23

If you know the code works after commenting out some of it, just keep commenting less code until you find the exact place causing you problems. Also check your error console as it's probably giving you some useful information. What does the line I messed with above look like after the PHP has run? - Jaspe

Also, even when I comment that one line out, it still causes the Javascript to crash - user1036272

I'd like to help diagnose your problem however without seeing the live code or a jsfiddle of the code I can't help much more. - Jaspe

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

respondido 08 nov., 11:23

Esto debería funcionar.

<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>

respondido 08 nov., 11:23

Instead of declaring (document).ready twice, try doing it like this:

<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>

respondido 08 nov., 11:23

Tener múltiples $(document).ready callbacks is completely acceptable, and not responsible for the symptom he's describing. - meagar

I fixed the problem. It turned out that Javascript was crashing due to the php code being outputted inside. I had to put that code into a seperate division then have it copied and appended. - user1036272

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.