2013-03-06 74 views
0

我有這個功能可以顯示或隱藏表顯示和隱藏表使用JQuery

function show_ticket_type(){ 
if ($("#TicketType").val("select-type")){ 
     $("#tow_way").hide() 
     $("#one_way").hide() 
} 
else if ($("#TicketType").val("one-way")){ 
     $("#tow_way").hide() 
     $("#one_way").show()  
} 
else{ 
     $("#tow_way").show() 
     $("#one_way").hide()  
} 

}

和表

<table width="100%" border="0" id="tow_way"> 
    <tr> 
    <th width="120"> <div align="center"><strong>Airlines</strong></div></th> 
    <th width="100"> <div align="center"><strong>Type</strong></div></th>  
    <th width="100"> <div align="center"><strong>From</strong></div></th> 
    <th width="100"> <div align="center"><strong>To</strong></div></th>  
    <th width="80"> <div align="center"><strong>Departure</strong></div></th> 
    <th width="100"> <div align="center"><strong>Returning</strong></div></th>  
    </tr> 
</tbale> 
<table width="100%" border="0" id="one_way"> 
    <tr> 
    <th width="120"> <div align="center"><strong>Airlines</strong></div></th> 
    <th width="100"> <div align="center"><strong>Type</strong></div></th>  
    <th width="100"> <div align="center"><strong>From</strong></div></th> 
    <th width="100"> <div align="center"><strong>To</strong></div></th>  
    <th width="80"> <div align="center"><strong>Departure</strong></div></th> 
    </tr> 
</tbale> 

這是選擇類型時選擇價值,我們必須呼叫功能隱藏或顯示錶格

<select id="TicketType" name="TicketType" onChange="show_ticket_type()"> 
    <option value="select-type">-- Select --</option> 
    <option value="one-way">One Way</option> 
    <option value="tow-way">Return</option> 
</select> 

如果我選擇單程我們必須隱藏表tow_way和顯示錶one_way當選擇拖拉方式我們必須要隱藏表one_way和顯示錶tow_way否則,我們必須隱藏拖表

我的代碼中的錯誤在哪裏?

+0

什麼錯誤信息你好嗎? – Mortalus 2013-03-06 20:20:17

+1

你的代碼中有很多錯別字,從'tow-way'到'tbale'。 – MattDiamant 2013-03-06 20:26:41

回答

0
$('#TicketType').change(function(){ 
    $("#two_way").toggle(this.value == "two-way") 
    $("#one_way").toggle(this.value == "one-way") 
}); 
  • 固定的類型 - tow =>two,因爲拖的方式聽起來令人毛骨悚然...... :)
+0

我沒有投票。但是,用這種方法是否有可能獲得完整的需求(選擇3個選項)?將很高興知道如何! – hop 2013-03-06 20:50:27

+0

@hop,它已經完成了所有的三個選項,但它是通過兩個條件而不是三個來完成的...... – gdoron 2013-03-06 20:52:53

0

你在想什麼,你在這裏幹嘛?

$("#TicketType").val("select-type") 

您正試圖設置該值而不是讀取該值。

你應該做if($("#TicketType").val() === "select-type")

此外,你應該介意你的拼寫:)

+0

謝謝@hop爲您的解決方案 – 2013-03-06 20:37:49