2013-02-21 63 views
0

有人對SO幫我寫這個劇本的選擇框,它會顯示在下拉負荷運行腳本

function showNext(opt) { 
    takeAway() 
    var optMap={ 
     "Picked Up":"#pickedup", 
     "Bus to alternate":"#altad2,#altad1", 
     "Walk to alternate":"#altad2,#altad1", 
    } 
    $(optMap[opt]).css("display","") ; 
} 

function takeAway(){ 
    $("#pickedup,#altad2,#altad1").css("display", "none"); 
} 

我需要的東西,會爲這項工作的onload運行基於選擇的選擇隱藏的行也因此,如果事情已被選中的行顯示

下面是將由腳本

<tr class="~[evenoddrow]"> 
      <td class="bold">In the event of early dismissal I would like my child to:</td><td id="Emergclose_Action">~([01]Emergclose_Action)</td><td class="gwCen"></td> 
      <td><select name="eReg|Emergclose_Action" id="eReg|Emergclose_Action" onChange="showNext(value);"> 
        <option></option><option value="Walk home as normal">Walk home as normal</option><option value="Ride the bus as normal">Ride the bus as normal</option> 
<option value="Picked Up">Be picked up immediately after dismissal</option><option value="Bus to alternate">Ride the bus to an alternate address</option><option value="Walk to alternate">Walk to an alternate address</option> 
       </select>   
      </td> 
     </tr> 

我嘗試以下影響的選擇並沒有工作..

function showNext(opt) { 
     takeAway() 

     var optMap={ 
      "Picked Up":"#pickedup", 
      "Bus to alternate":"#altad2,#altad1", 
      "Walk to alternate":"#altad2,#altad1", 
       } 
      $(optMap[opt]).css("display","") ;  

} 
function takeAway(){ 
    $("#pickedup,#altad2,#altad1").css("display", "none"); 
} 
$(function(){ 
    $('#eReg|Emergclose_Action').trigger('onchange'); 
}); 
+0

在我看來,有一些缺失的部分。 'showect'方法是否附加到'#eReg | Emergclose_Action'的onchange事件上? – UpHelix 2013-02-21 19:44:39

+0

我也試過用你的例子UpHelix,那也沒有工作 – user2096551 2013-02-21 19:49:12

+0

你的問題很混亂。你想要哪個確切的方法運行onload,並且該方法是否附加到DOM元素? – UpHelix 2013-02-21 19:52:53

回答

0

嘗試從您選擇的元素中刪除onChange="showNext(value);"並更改ID。在選擇器中使用管道|並不好,requires a work-a-round沒有它更容易。改爲用下劃線_代替。

所以你的選擇HTML是:

<select name="eReg|Emergclose_Action" id="eReg_Emergclose_Action"> 

然後做的JavaScript是這樣的:

$(function(){ 

    //anything in here will run after the page loads 

    //assign showNext to the change event 
    $('#eReg_Emergclose_Action').change(function(){ 
     showNext($(this).val()); 
    }).change();//manually trigger the change event after its attached 

}); 
+0

我認爲的那種..我編輯原始帖子以包括選擇。也許這將有助於 – user2096551 2013-02-21 19:32:02

+0

更新回答 – UpHelix 2013-02-21 19:34:45

0

,你有,如果有人已經選定的是以前在頁面加載時運行該腳本,可以使用Jquery

進行優化,如下所示:$('#eReg | Emergclose_Action')。trigger('onchange');

+0

感謝您的提示:)不是我問的問題,但有幫助的但: ) – user2096551 2013-02-21 19:39:18

+0

或者也許這是我誤讀仍然無法讓它工作:) – user2096551 2013-02-21 19:51:02