2013-07-28 62 views
0

使用jQuery,給定多個複選框,如果特定複選框被選中/取消選中,我們可以打開/關閉字段集。 此外,如果默認選中複選框,則會在加載頁面時顯示相應的字段集。 請參閱http://jsfiddle.net/Hbmpk/1/ 但是,如果在Typo3中完成此操作,則字段集不會在頁面加載時顯示。使用jQuery .ready()來操作typo3中的鏈接複選框

這裏是Typo腳本:

page.includeJSlibs.jquery.external = 1 
page.includeJSlibs.jquery = http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js 

page.headerData.10 = TEXT 
page.headerData.10.value (
    <script type="text/JavaScript"> 
    #show fieldset on page load if checkbox checked 
    $(document).ready(function() {$('#showfruit').toggle($('#fruitid').prop('checked')); }); 

    #toggle fieldsets 
    $(window).load(function(){ 
    $('#fruitid').change(function(e) { 
    $('#showfruit').toggle(this.checked); 
}); 
$('#vegid').change(function(e) { 
    $('#showveg').toggle(this.checked); 
}); 
}); 
    </script> 
) 

下面是HTML:

<form> 
Which food group do you like? 
<!-- Fruit is checked by default --> 
Fruit <input type="checkbox" name="nutrition[]" value="Fruit" id="fruitid" checked="checked"> 
Veges <input type="checkbox" name="nutrition[]" value="Vegetables" id="vegid"> 

<!-- toggle fieldsets if checkbox is checked --> 
<!-- display showfruit fieldset on page load --> 
<fieldset id="showfruit" style="display:none;"> 
You chose Fruit! Name one fruit: <input type="text" name= "afruit" /> 
</fieldset> 
<fieldset id="showveg" style="display:none;" > 
You chose Veges! Name one veg: <input type="text" name= "aveg" /> 
</fieldset> 
</form> 

這是企圖與鏈式複選框和輸入一起使用TYPO3-formhandler驗證的簡化;切換工作正常,但是當表單被提交,驗證並返回時(如果強制性問題未得到回答),則複選框保持選中狀態,但顯示的相應字段集現在不再顯示。

PS,jQuery的是感謝傑森P - jquery: toggle a fieldset based upon a specific checked checkbox in an array of multiple checkboxes

+0

聲音就像JS問題一樣。 $(document).ready(function(){$('#showfruit')。toggle($('#fruitid')。prop('checked'));});'在代碼中應該做任務,所以問題是,爲什麼沒有。您的瀏覽器中的js控制檯中是否出現錯誤?你可以發佈到實際頁面的鏈接嗎? – Michael

+0

感謝邁克爾,有時只需要提醒您檢查是否有明顯的錯誤。請參閱下面的答案。 – HowardK

回答

0

的TYPO3擴展jquerycolorbox也很活躍。 jquerycolorbox可能包含一個小於1.6的jquery版本,因此不支持.prop() 在jquerycolorbox之上包含更高版本的jquery並沒有幫助,而且本質上是不正確的做法。

取消激活jquerycolorbox幷包括jquery 1.10.2/jquery.min.js工作正常。

鑑於問題的網頁實際上使用TYPO3 formhandler驗證,也許是最好的解決辦法是一些Typo腳本,是以最初的形式提交結果,然後酌情確定整場集合,說###fruit_fieldset### ...