2012-03-28 50 views
3

我有一個窗體有很多選擇菜單,其中大多數是/否,並根據選定的選項,我顯示/隱藏一些高級選項。其中一個選擇菜單如下:JavaScript如何運行onLoad和onChange相同的功能

<td><%= f.select :CBIAvailable, ['Yes' , 'No'],{}, {:id=>"cbi_available_id", :class=>"cbi_available_class", :onChange=>"showHideOptions('cbi_available_id','cbi_options_id')", :onLoad=>"showHideOptions('cbi_available_id','cbi_options_id')"} %></td> 

當我從「有」到「無」或者相反,JavaScript函數將被正常調用showHideOptions改變,但我不能有函數被調用時,我重新加載表單。

任何人都可以告訴我我東錯了什麼?

感謝

UPDATE

<script language="javascript" type="text/javascript"> 
function showHideOptions(selectorId,optionsId) { 
    if (document.getElementById) { 
    var selector = document.getElementById(selectorId); 
    var options = document.getElementById(optionsId); 
    if (selector.value == 'Yes') { 
     options.style.display = 'block'; 
     return false; 
    } else { 
     options.style.display = 'none'; 
     return false; 
    } 
} 

window.onLoad = showHideOptions('cbi_available_id','cbi_options_id'); 
+0

我不明白。當窗體被重新加載時,Onload被調用。你在期待什麼? – 2012-03-28 06:14:23

+0

在我的情況下,:CBIAvailable ='No',所以我希望showHideOptions函數在我重新加載表單時執行,它應該顯示'No'的相關選項,但實際上表單會重新加載'Yes'選項。當我將選擇菜單更改爲「否」時,它工作正常,當我將其更改回「否」時,它也可以正常工作。問題是它沒有顯示正確的選項onload – rh4games 2012-03-28 06:23:14

回答

0

當你從遠程服務器接收和回傳你的迴應沒有<script type="javascript">... yourfunction()...</script>這可能發生。

每次獲得新的回覆時,您都應該發送腳本並將其取消或追加到您的html元素認可事件處理程序中。

另一個解決方案是使用jQuery並使用.live()事件。此事件將動態行爲附加到您的html。 我強烈建議你使用jQuery,因爲這個庫是生產環境中最常用的庫之一。

編輯

<script type="text/javascript" src="path_to_jquery.js"></script> 
<script type="text/javascript"> 
function showHideOptions(id1,id2){ 
    //Your code 
} 
$(document).ready(function() { 
    //This function waits for DOM load 
    //execute your function for first time 
    showHideOptions('cbi_available_id','cbi_options_id'); 
    //This selector you have to modify or show us generated html to 
    // choose the best selector for this purpose 
    $('.cbi_available_class').live('change',function(){ 
     //This will fire change event of html class="cbi_available_class" 
     showHideOptions('cbi_available_id','cbi_options_id'); 
    }); 
}); 
</script> 
+0

謝謝r.piesnikowski。 我有 能否請您與使用樣品jQuery腳本幫助/生活()? – rh4games 2012-03-28 06:29:42

+0

請注意,jquery live()在JQuery 1.7中已棄用,並從1.9 – Lindow 2017-06-03 09:41:19

1
function yourFunction(){ 

    //get that select element and evaluate value 
    //do you change stuff here 
} 

window.onload = yourFunction; //this gets fired on load 

//"select" is your element, 
//fetched by methods like document.getElementById(); 

select.onchange = yourFunction; //this gets fired on change 

//you can also use attachEvent (IE) or addEventListener (Others) 

這裏是a working demo

<select id="testSelect"> 
    <option value="yes">YES</option> 
    <option value="no">NO</option> 
</select>​​​​​ 

function getOption() { 
    alert('foo'); 
} 

var select = document.getElementById('testSelect'); 
select.onchange = getOption; 
window.onload = getOption; 

+0

中刪除,謝謝Joseph。我更新了我提出的問題,並提出了更改,但重新加載表單時仍然無法正常工作。當我將選擇的選項從「是」更改爲「否」和相反時,它確實工作正常。 – rh4games 2012-03-28 06:45:28

+0

更新了我的回答 – Joseph 2012-03-28 06:48:04

+0

仍然無法正常工作。實際上,使用原始語法時,函數會觸發onload(我通過在函數內部添加document.wrtie進行測試),但它不會完成剩下的工作,例如當我選擇「是」或「否」 – rh4games 2012-03-28 06:55:49