Colorbox call with dynamically created selectors
I am getting error when generating javascript dynamically like so:
$(document).ready(function(){
$("#$this->AddURLSegment").colorbox();
}
HTML:
<a href="<?php echo $AddURLSegment;?>?width=500&height=500&iframe=false" id="<?php rcho $AddURLSegment;?>">Add</a>
Error: First click on Add:
uncaught exception: Syntax error, unrecognized expression: #
Second time:
ab.html(a.close).show is not a function
uncaught exception: Syntax error, unrecognized expression: #
and 2 overlays open, one good and one broken.
! When i hardcode the selectors (ID name) like so...., it works.
$("#add").colorbox();
What is gong on here!?
EDIT:
Im doing this in silverstripe cms
The javascript (noconflict) is generated in Controller::init()
Requirements::customScript('
$j(document).ready(function(){
$j("#'.$this->AddURLSegment.'").colorbox();
});
');
The Html 开发者_开发知识库stuff is in View.
*SilverStripe is based on MVC paradigm.
Your problem is that you are trying to access $this->AddURLSegment from within Controller::init(). The AddURLSegment method is not available within the controller class. You need to call that in a subclass of controller. Most SilverStripe pages look like this:
Page extends SiteTree { }
Page_Controller extends ContentController { public init() { Requirements::customScript(...); } }
So, add the customScript into the controller of the page type you are using. That should make it work.
精彩评论