单击按钮后加载javascript文件 [英] Load javascript file after button click

查看:95
本文介绍了单击按钮后加载javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一组文本框在我的JavaScript文件中定义一些数据.因此,在一个文本框中,我输入了一个团队名称"Team name",并且javascript文件使用此文本框存储了一个团队的名称,以便打印出一组锦标赛括号.

I am trying to use a set of textboxes to define some data within my JavaScript file. So in a textbox I enter a Team name "Team name", and the javascript file uses this textbox to store the name of a team in order to print a set of tournament brackets.

我的<head>中包含了javascript文件,因此在我实际在文本框中输入数据之前就已加载该文件.我在html中的按钮上有以下代码:

The javascript file is included in my <head> so it loads before I have actually entered the data in the text boxes. I have this code for a button in the html like so:

script(type='text/javascript')
    $('#buttontest').click(function() {
        $('div.bracket').show();
        $('div.teamList').hide();
    });

因此,当按下按钮时,所有文本框均被隐藏,带有用户定义的团队名称的方括号应出现....这是我的javascript文件末尾的代码:

So when the button is pressed, the textboxes are all hidden, and the brackets with the user-defined team names should appear....here is the code at the end of my javascript file:

$(function() {
    $('#singleElim8').bracket({
        init: singleElim8Data
    })
  $('.bracket').hide();
});

所以在div.bracket中,我有JavaScript使用的#singleElim8类.但是,我将如何更改它,以便此javascript初始化方括号仅在单击按钮后才运行?这样,在文本框被填充后,方括号就会呈现出来.任何帮助.

So within div.bracket I have the class #singleElim8 which the javascript uses. But how would I change it so that this javascript to initialize the brackets only runs once the button has been clicked? That way the brackets render AFTER the textboxes have been filled in. Any help appreciated.

推荐答案

只需使用 getScript方法jQuery的

Just use the getScript method of jQuery

使用非常简单

$( '#buttontest' ).on( 'click', function() {
    $.getScript( 'url to your js file', function( data, textStatus, jqxhr ) {
        // do some stuff after script is loaded
    } );
} );

这篇关于单击按钮后加载javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆