problem binding functions
I call a section of another page in to the parent page like this:
$(function() {
$("#subscribe").click(function () {
$("#singleContent").load('purchaseForm.php #Content');
});
});
no problem there. I am having an issue getting functions inside of 'purchaseForm.php' to run. I've tried the ".live" function, but can only bind that to events like click. I need to hide some things and get this code to run:
$('[class^=toggle-item]').hide();
$('[class^=link]').live('click', function(e) {
$('.toggle-item-' + this.className).slideToggle("fast");
$('span',this).text(function(i,txt) {
return txt === "Learn More..." ? "Close" : "Learn More...";
e.preventDefault();
});
});
$(function() {
var $add = $('input.add');
$add.live('click', function() {
$add.removeAttr('checked');
$(this).attr('check开发者_如何学编程ed', true);
});
});
How do I bind the above functions to the .load function?
You could do something like this:
$(function() {
$("#subscribe").click(function () {
$("#singleContent").load('purchaseForm.php #Content', function()
{
$('[class^=toggle-item]').hide();
$('[class^=link]').live('click', function(e) {
$('.toggle-item-' + this.className).slideToggle("fast");
$('span',this).text(function(i,txt) {
return txt === "Learn More..." ? "Close" : "Learn More...";
e.preventDefault();
});
});
$(function() {
var $add = $('input.add');
$add.live('click', function() {
$add.removeAttr('checked');
$(this).attr('checked', true);
});
});
});
});
});
That's a jQuery bug I reported a while ago. To my knowledge, it's not fixed yet.
I'll find the bug and link it. Specifically, when you use ".load()" with a "context" like that (the selector following the URL), it does not run <script>
tags in the content that's loaded.
edit — ok here is the bug.
To make your setup work, probably the best thing would be to have that code in the surrounding page that loads the content.
精彩评论