简体   繁体   English

jQuery load():将javascript加载到div中,多次调用一个函数

[英]jquery load(): loading a javascript in to div, calls a function multiple times

I have a main div called #Content and I replace a data in the #Content based the menu item clicked: 我有一个名为#Content的主div,并根据单击的菜单项替换了#Content中的数据:

here is the code: 这是代码:

$('.nav-button').on('click', function(e){
e.preventDefault();
$('#content').load($(this).attr('href'));
});

You're running this binding statement every time you click "Transport": 每次单击“运输”时,您都在运行此绑定语句:

$(".tsave").live('click',function() {

Try changing it to this: 尝试将其更改为此:

$(".tsave").unbind('click');
$(".tsave").live('click',function() {

jQuery will append binding statements every time you call them - so, if you bind a click event 10 times to one object, it will run that event 10 times on the event. 每次调用jQuery时,jQuery都会追加绑定语句-因此,如果将click事件绑定到一个对象10次,它将在该事件上运行该事件10次。

Also change this: 还要更改此:

$(".save").live('click',function() {

To: 至:

$(".save").unbind('click'); 
$(".save").live('click',function() {

Found answer here:http://forum.jquery.com/topic/jquery-load-loading-a-javascript-in-to-div-calls-a-function-multiple-times 在这里找到答案:http://forum.jquery.com/topic/jquery-load-loading-a-javascript-in-to-div-calls-a-function-multiple-times

just made a check not to load the script if it is loaded previously. 刚刚检查了是否不加载脚本(如果先前已加载)。 using this simple script: 使用以下简单脚本:

if (! window.loadedThisScript){
window.loadedThisScript = true
// the whole part of the script that you only want to load one time.
}

credit to: http://forum.jquery.com/user/jakecigar 致谢: http : //forum.jquery.com/user/jakecigar

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

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