2014-02-21 27 views
0

我在1个文件中放置了多个2个JS脚本,虽然它们在HTML文件中绕着时很好地独立运行,但第二个在我调用它时根本不起作用从外部的JS文件。放置多个JS脚本1个JS文件

这里的JS代码:

// start of open the name div 

$(function() { 
    $('.reply_submit_name_open').click(function(){ 
     $(this).siblings('.reply_submit_name').fadeIn(); 
     $(this).fadeOut(); 
    }); 

    $('.check_author').click(function(){ 
     if($('.check_author').is(":checked")) { 
      $('.name_input').val($(this).val()); 
      $('.name_input').attr('readonly', true); 
     } else { 
      $('.name_input').attr('readonly', false); 
     } 
    }); 
}); 

// end of opening name div 


// start of posting replies 
var base_url = '<?php print base_url();?>'; 
var interview_id = '<?php echo $this->uri->segment(3)?>'; 

$(function() { 
    $('.reply_button').click(function(){ 

     var this_a = $(this); 
     var reply = $(this).parents().siblings('textarea').val(); 
     var username =$(this).siblings().find('input').val(); 
     var parent_comment_id=$(this).closest('form').attr('id'); 
     var last_reply_id=$(this).closest('.reply_form_div').siblings('.replies').children().last('.comment').attr('id'); 

     $.ajax({ 
      type:"POST", 
      url: base_url + "comment_upload/reply_upload", 
      data:{parent_comment_id : parent_comment_id, interview_id: interview_id, reply:reply, username: username}, 
      dataType: "json", 
      success: function(data,status){ 
       if(data.state == 'succ') { 
        this_a.html('Success'); 
        $('.reply_button').closest('.reply_form_div').siblings('.replies').append(data.new_reply); 
       } else { 
        this_a.html('Bad'); 
       } 
      } 
     }); 
    }); 
}); 
// end of posting replies 

是什么原因?欣赏任何建议。

+0

我没有看到他们应该发生冲突的任何理由。你怎么“打电话给他们”?没有命名的函数可以调用。 – Barmar

+0

这是在PHP类中吗?因为你在调用'$ this'。 JS和PHP的输出是否有效? – vonUbisch

+0

我打电话给他们基于被点击的按钮。 – user3192948

回答

0

你已经有了一堆PHP,如果你把它移动到一个外部的.js文件中,除非你已经配置了你的服务器来通过php运行所有的.js文件,否则肯定不会被评估。

+0

我认为你是对的。有没有一种方法,我可以以某种方式将这些变量从HTML传递到外部JS? – user3192948

+0

它们是全局变量,你只需要将两个''行放入PHP文件中,它们就可以被所有随后包含的JavaScript使用。 – meagar