2017-04-07 60 views
0

我有我的代碼HTML 4個單選按鈕:我怎麼知道哪個單選按鈕是在文件選擇HTML,嫩枝

<input id="spa-price" name="price" class="w3-radio" value="Spare {{ price.getSparePrice }}" type="radio"> 
<input id="spa-price" name="price" class="w3-radio" value="Repair {{ price.getRepairPrice }}" type="radio"> 
<input id="spa-price" name="price" class="w3-radio" value="Test {{ price.getTestPrice }}" type="radio"> 
<input id="spa-price" name="price" class="w3-radio" value="Std-Exchange {{ price.getExchangePrice }}" type="radio"> 

在後,我要輸出的隱藏字段與選定的值單選按鈕,如:

<input type="hidden" name="lt" value="{{ price.getLt }}"> 

我目前的代碼:

{% if $_POST['price'] == 'Spare' %} 
<input type="hidden" name="lt" value="{{ price.getLt }}"> 

{% elseif $_POST['price'] == 'Repair' %} 
<input type="hidden" name="lt" value="{{ 10 }}"> 

{% elseif $_POST['price'] == 'Test' %} 
<input type="hidden" name="lt" value="{{ 10 }}"> 

{% else $_POST['price'] == 'Exchange' %} 
<input type="hidden" name="lt" value="{{ price.getLt }}"> 

目前,這是行不通的。

我的錯誤是什麼?謝謝。

+1

恐怕你的問題是有點不清楚我。主要是錯誤的一部分。你拿到了嗎?如果是的話,請顯示它。或者你想知道如何檢索這個錯誤? – KevinTheGreat

+0

對不起,我的問題是如何在文件html.twig中對這些無線電對象進行測試。 – sirine

+0

我改變了它,但它沒有工作:{%if price.Spare == 1%} > {%elseif price.Test == 1%} {%elseif price.Repair == 1%} {%else%} {%endif% } – sirine

回答

2

首先,你已經給每個複選框,你做了相同的id這是不好的做法。在HTML中,每個id應該有一個唯一的名稱。我建議你對名稱也一樣。但是現在是可選

因此改變你id's此:

<input id="spare-price" name="price" class="w3-radio" value="Spare {{ price.getSparePrice }}" type="radio"> 
<input id="repair-price" name="price" class="w3-radio" value="Repair {{ price.getRepairPrice }}" type="radio"> 
<input id="test-price" name="price" class="w3-radio" value="Test {{ price.getTestPrice }}" type="radio"> 
<input id="exchange-price" name="price" class="w3-radio" value="Std-Exchange {{ price.getExchangePrice }}" type="radio"> 

您可以使用JavaScript來檢查,如果複選框被選中

{% block javascripts%} 
    <script> 
     function validate(){ 
      var spare= document.getElementById('spare-price'); 
      if (spare.checked){ 
      alert("checked") ; 
      }else{ 
      alert("You didn't check spare-price! Let me check it for you.") 
      } 
     } 
    </script> 
    {% endblock %} 
+0

我很抱歉這個打擾。我在文件HTML.twig中添加了一個腳本:

相關問題