2016-03-14 35 views
0

我使用的引導中,我有一個輸入的複選框這樣的:流星加載JavaScript函數DOM後載入

<input type="checkbox" id="toggle" name="toggle"> 

而且我使用Bootstrap Switch使用它們的功能將其轉換成一個開關:

Meteor.startup(function() { 
    $("[name='toggle']").bootstrapSwitch(); 
}); 

但是,問題是交換機只加載一半的時間。我認爲這是因爲該函數在複選框加載到DOM之前被調用。我認爲把它放入Meteor啓動函數可以修復它,但它不會。如何確保在加載複選框後加載此開關?我試過Template.body.onRendered,它也沒有工作。

+0

嘗試使用'Template.body.onCreated' – chackerian

+0

@jswny您想使用'Template.X.onRendered',其中'X'是複選框的直接父模板。這應該適用於所有情況,除非元素是有條件呈現的(在這種情況下,更多技巧是必要的,我會指出你的相關問題)。 –

+0

@chackerian和David Weldon我已經嘗試了這兩件事,但都沒有成功。 –

回答

1

考慮,上面提到的代碼模板myTemplate

my_template.html

<template name="myTemplate"> 
    <input type="checkbox" id="toggle" name="toggle"/> 
</template> 

那麼你my_template.js將有

my_template.js

if(Meteor.isClient) { 
    Template.myTemplate.onRendered(function() { 
     $("[name='toggle']").bootstrapSwitch(); 
    }); 
}