2011-11-15 42 views
-1

嗯,我有一個HTML格式的文件。當我將它作爲一個jsp文件進行更改並放入某些jsp代碼時,javascript停止響應並且沒有任何反應。這只是一個貨幣轉換器,交換按鈕通過JavaScript進行管理。JSP和JavaScript衝突我認爲

下面是代碼:

<html> 
<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#swap').click(function() { 
        s1=$('#to').val();   
        s0=$('#from').val();   
        $('#to').val(s0);   
        $('#from').val(s1); 
      }); 
     }); 
</script> 
</head> 
<body> 

    <%@ page import="java.util.*" %> 

    <div class="background"></div> 

    <div id="heading"> 
     <% 
     String currency = (String)request.getAttribute("currency"); 
     if(currency == null) 
     { 
      out.print("live currency converter"); 
     }else{ 
      out.println("Currency Converted: " +currency); 
     } 
     %> 
    </div> 

<div id="currencyBox"> 
<form method="POST" action="CurrencyConverter.do"> 

    <div class="data"> 
     <label for="from">Convert:</label> 
     <input type="text" name="amount" id="amount" value="1" /> 
    </div> 
    <div class="data"> 
     <label for="fromCurrency">From Currency:</label> 
     <select name="from" id="from"> 
      <option selected="" value="EUR">Euro - EUR</option> 
      <option value="USD">United States Dollars - USD</option> 
      <option value="GBP">United Kingdom Pounds - GBP</option> 
     </select> 
    </div> 

    <div class="data"> 
     <label for="to">To Currency:</label> 
     <select name="to" id="to"> 
      <option value="USD">United States Dollars - USD</option> 
      <option value="GBP">United Kingdom Pounds - GBP</option> 
      <option value="CAD">Canada Dollars - CAD</option> 
     </select> 
    </div> 
    <div class="data"> 
     <input type="submit" name="submit" id="submit" value="Convert the input"> 
     <input type="button" name="swap" id="swap" value="Swap values"> 
    </div> 
</form> 
<form method="GET" action="CodeGrabber.do"><input type="submit" value="Grab the code!"></form> 
<form method="GET" action="ListenTest.do"><input type="submit" value="On line report!"></form> 

</div> 
</body> 
</html> 

當我刪除了<% %>部分的代碼在上面的作品就像一個魅力的JavaScript。 我不熟悉jsp和servlets,我有一個php背景!

這裏是開發模式我的輸出

<html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 

    <title>currency converter</title> 
    <meta name="description" content="currency converter"> 
    <link rel="stylesheet" href="lib/c/styles.css"> 
    <meta property="og:site_name" content="fizaboun/3w lesson"/> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#swap').click(function() { 
    Â   s1=$('#to').val(); Â 
    Â   s0=$('#from').val(); Â 
    Â   $('#to').val(s0); Â 
    Â   $('#from').val(s1); 
      }); 
     }); 
</script> 
</head> 
<body> 



    <div class="background"></div> 

    <div id="heading"> 
      live currency converter 
    </div> 

<div id="currencyBox"> 
<form method="POST" action="CurrencyConverter.do"> 

    <div class="data"> 
     <label for="from">Convert:</label> 
     <input type="text" name="amount" id="amount" value="1" /> 
    </div> 
    <div class="data"> 
     <label for="fromCurrency">From Currency:</label> 
     <select name="from" id="from"> 
      <option selected="" value="EUR">Euro - EUR</option> 
      <option value="USD">United States Dollars - USD</option> 
      <option value="GBP">United Kingdom Pounds - GBP</option> 
      <option value="CAD">Canada Dollars - CAD</option> 
      <option value="AUD">Australia Dollars - AUD</option> 
      <option value="JPY">Japan Yen - JPY</option> 
      <option value="INR">India Rupees - INR</option> 
      <option value="NZD">New Zealand Dollars - NZD</option> 
      <option value="CHF">Switzerland Francs - CHF</option> 
      <option value="ZAR">South Africa Rand - ZAR</option> 
      <option value="DZD">Algeria Dinars - DZD</option> 
      <option value="USD">America (United States) Dollars - USD</option> 
      <option value="ARS">Argentina Pesos - ARS</option> 
      <option value="AUD">Australia Dollars - AUD</option> 
      <option value="BHD">Bahrain Dinars - BHD</option> 
      <option value="BRL">Brazil Reais - BRL</option> 
      <option value="BGN">Bulgaria Leva - BGN</option> 
      <option value="CAD">Canada Dollars - CAD</option> 
      <option value="CLP">Chile Pesos - CLP</option> 
      <option value="CNY">China Yuan Renminbi - CNY</option> 
      <option value="CNY">RMB (China Yuan Renminbi) - CNY</option> 
      <option value="COP">Colombia Pesos - COP</option> 
      <option value="CRC">Costa Rica Colones - CRC</option> 
      <option value="HRK">Croatia Kuna - HRK</option> 
      <option value="CZK">Czech Republic Koruny - CZK</option> 
      <option value="DKK">Denmark Kroner - DKK</option> 
      <option value="DOP">Dominican Republic Pesos - DOP</option> 
      <option value="EGP">Egypt Pounds - EGP</option> 
      <option value="EEK">Estonia Krooni - EEK</option> 
      <option value="EUR">Euro - EUR</option> 
      <option value="FJD">Fiji Dollars - FJD</option> 
      <option value="HKD">Hong Kong Dollars - HKD</option> 
      <option value="HUF">Hungary Forint - HUF</option> 
      <option value="ISK">Iceland Kronur - ISK</option> 
      <option value="INR">India Rupees - INR</option> 
      <option value="IDR">Indonesia Rupiahs - IDR</option> 
      <option value="ILS">Israel New Shekels - ILS</option> 
      <option value="JMD">Jamaica Dollars - JMD</option> 
      <option value="JPY">Japan Yen - JPY</option> 
      <option value="JOD">Jordan Dinars - JOD</option> 
      <option value="KES">Kenya Shillings - KES</option> 
      <option value="KRW">Korea (South) Won - KRW</option> 
      <option value="KWD">Kuwait Dinars - KWD</option> 
      <option value="LBP">Lebanon Pounds - LBP</option> 
      <option value="MYR">Malaysia Ringgits - MYR</option> 
      <option value="MUR">Mauritius Rupees - MUR</option> 
      <option value="MXN">Mexico Pesos - MXN</option> 
      <option value="MAD">Morocco Dirhams - MAD</option> 
      <option value="NZD">New Zealand Dollars - NZD</option> 
      <option value="NOK">Norway Kroner - NOK</option> 
      <option value="OMR">Oman Rials - OMR</option> 
      <option value="PKR">Pakistan Rupees - PKR</option> 
      <option value="PEN">Peru Nuevos Soles - PEN</option> 
      <option value="PHP">Philippines Pesos - PHP</option> 
      <option value="PLN">Poland Zlotych - PLN</option> 
      <option value="QAR">Qatar Riyals - QAR</option> 
      <option value="RON">Romania New Lei - RON</option> 
      <option value="RUB">Russia Rubles - RUB</option> 
      <option value="SAR">Saudi Arabia Riyals - SAR</option> 
      <option value="SGD">Singapore Dollars - SGD</option> 
      <option value="SKK">Slovakia Koruny - SKK</option> 
      <option value="ZAR">South Africa Rand - ZAR</option> 
      <option value="KRW">South Korea Won - KRW</option> 
      <option value="LKR">Sri Lanka Rupees - LKR</option> 
      <option value="SEK">Sweden Kronor - SEK</option> 
      <option value="CHF">Switzerland Francs - CHF</option> 
      <option value="TWD">Taiwan New Dollars - TWD</option> 
      <option value="THB">Thailand Baht - THB</option> 
      <option value="TTD">Trinidad and Tobago Dollars - TTD</option> 
      <option value="TND">Tunisia Dinars - TND</option> 
      <option value="TRY">Turkey Lira - TRY</option> 
      <option value="AED">United Arab Emirates Dirhams - AED</option> 
      <option value="GBP">United Kingdom Pounds - GBP</option> 
      <option value="USD">United States Dollars - USD</option> 
      <option value="VEB">Venezuela Bolivares - VEB</option> 
      <option value="VND">Vietnam Dong - VND</option> 
      <option value="ZMK">Zambia Kwacha - ZMK</option> 
     </select> 
    </div> 

    <div class="data"> 
     <label for="to">To Currency:</label> 
     <select name="to" id="to"> 
      <option value="USD">United States Dollars - USD</option> 
      <option value="GBP">United Kingdom Pounds - GBP</option> 
      <option value="CAD">Canada Dollars - CAD</option> 
      <option value="AUD">Australia Dollars - AUD</option> 
      <option value="JPY">Japan Yen - JPY</option> 
      <option value="INR">India Rupees - INR</option> 
      <option value="NZD">New Zealand Dollars - NZD</option> 
      <option value="CHF">Switzerland Francs - CHF</option> 
      <option value="ZAR">South Africa Rand - ZAR</option> 
      <option value="DZD">Algeria Dinars - DZD</option> 
      <option value="USD">America (United States) Dollars - USD</option> 
      <option value="ARS">Argentina Pesos - ARS</option> 
      <option value="AUD">Australia Dollars - AUD</option> 
      <option value="BHD">Bahrain Dinars - BHD</option> 
      <option value="BRL">Brazil Reais - BRL</option> 
      <option value="BGN">Bulgaria Leva - BGN</option> 
      <option value="CAD">Canada Dollars - CAD</option> 
      <option value="CLP">Chile Pesos - CLP</option> 
      <option value="CNY">China Yuan Renminbi - CNY</option> 
      <option value="CNY">RMB (China Yuan Renminbi) - CNY</option> 
      <option value="COP">Colombia Pesos - COP</option> 
      <option value="CRC">Costa Rica Colones - CRC</option> 
      <option value="HRK">Croatia Kuna - HRK</option> 
      <option value="CZK">Czech Republic Koruny - CZK</option> 
      <option value="DKK">Denmark Kroner - DKK</option> 
      <option value="DOP">Dominican Republic Pesos - DOP</option> 
      <option value="EGP">Egypt Pounds - EGP</option> 
      <option value="EEK">Estonia Krooni - EEK</option> 
      <option value="EUR">Euro - EUR</option> 
      <option value="FJD">Fiji Dollars - FJD</option> 
      <option value="HKD">Hong Kong Dollars - HKD</option> 
      <option value="HUF">Hungary Forint - HUF</option> 
      <option value="ISK">Iceland Kronur - ISK</option> 
      <option value="INR">India Rupees - INR</option> 
      <option value="IDR">Indonesia Rupiahs - IDR</option> 
      <option value="ILS">Israel New Shekels - ILS</option> 
      <option value="JMD">Jamaica Dollars - JMD</option> 
      <option value="JPY">Japan Yen - JPY</option> 
      <option value="JOD">Jordan Dinars - JOD</option> 
      <option value="KES">Kenya Shillings - KES</option> 
      <option value="KRW">Korea (South) Won - KRW</option> 
      <option value="KWD">Kuwait Dinars - KWD</option> 
      <option value="LBP">Lebanon Pounds - LBP</option> 
      <option value="MYR">Malaysia Ringgits - MYR</option> 
      <option value="MUR">Mauritius Rupees - MUR</option> 
      <option value="MXN">Mexico Pesos - MXN</option> 
      <option value="MAD">Morocco Dirhams - MAD</option> 
      <option value="NZD">New Zealand Dollars - NZD</option> 
      <option value="NOK">Norway Kroner - NOK</option> 
      <option value="OMR">Oman Rials - OMR</option> 
      <option value="PKR">Pakistan Rupees - PKR</option> 
      <option value="PEN">Peru Nuevos Soles - PEN</option> 
      <option value="PHP">Philippines Pesos - PHP</option> 
      <option value="PLN">Poland Zlotych - PLN</option> 
      <option value="QAR">Qatar Riyals - QAR</option> 
      <option value="RON">Romania New Lei - RON</option> 
      <option value="RUB">Russia Rubles - RUB</option> 
      <option value="SAR">Saudi Arabia Riyals - SAR</option> 
      <option value="SGD">Singapore Dollars - SGD</option> 
      <option value="SKK">Slovakia Koruny - SKK</option> 
      <option value="ZAR">South Africa Rand - ZAR</option> 
      <option value="KRW">South Korea Won - KRW</option> 
      <option value="LKR">Sri Lanka Rupees - LKR</option> 
      <option value="SEK">Sweden Kronor - SEK</option> 
      <option value="CHF">Switzerland Francs - CHF</option> 
      <option value="TWD">Taiwan New Dollars - TWD</option> 
      <option value="THB">Thailand Baht - THB</option> 
      <option value="TTD">Trinidad and Tobago Dollars - TTD</option> 
      <option value="TND">Tunisia Dinars - TND</option> 
      <option value="TRY">Turkey Lira - TRY</option> 
      <option value="AED">United Arab Emirates Dirhams - AED</option> 
      <option value="GBP">United Kingdom Pounds - GBP</option> 
      <option value="USD">United States Dollars - USD</option> 
      <option value="VEB">Venezuela Bolivares - VEB</option> 
      <option value="VND">Vietnam Dong - VND</option> 
      <option value="ZMK">Zambia Kwacha - ZMK</option> 
     </select> 
    </div> 
    <div class="data"> 
     <input type="submit" name="submit" id="submit" value="Convert the input"> 
     <input type="button" name="swap" id="swap" value="Swap values"> 
    </div> 
</form> 
<form method="GET" action="CodeGrabber.do"><input type="submit" value="Grab the code!"></form> 
<form method="GET" action="ListenTest.do"><input type="submit" value="On line report!"></form> 

</div> 
</body> 
</html> 
+0

這就是即使Java看起來像一個普通的舊PHP腳本:)。 – FloydThreepwood

+1

一眼就看不出是什麼原因造成的。發佈頁面的實際瀏覽器源輸出。正如Balus所說,JSP不是你的根本問題。 – Matthew

+0

根據您對我已刪除答案的評論,您尚未發佈真實的SSCCE。你已經發布了一個半透明的,過度簡化和不完整的代碼片段,並不代表你真正的問題。作爲示例,請在演示之前自行運行代碼。 – BalusC

回答

1

我不知道你所說的「開發商模式」的意思 - 就是你所看到的,當你在瀏覽器中加載頁面,然後單擊「查看源文件「? - 但由於所有「Â」字符,以下JavaScript無效。他們來自哪裏?

$(document).ready(function() { 
     $('#swap').click(function() { 
    Â   s1=$('#to').val(); Â 
    Â   s0=$('#from').val(); Â 
    Â   $('#to').val(s0); Â 
    Â   $('#from').val(s1); 
      }); 
    }); 

我沒有看到任何方式,在<% %>標籤Java代碼可能會造成任何問題。

+0

是的,這也很奇怪,但只有當我將代碼放入代碼<% %>剛剛消失,JavaScript再次順利運行......我無法在我的IDE(尾聲)中看到這些字符。他們不存在那裏。 –

+0

解決它只是刪除新行 –