2012-11-25 11 views
0

我有一個表格檢查是否已經選擇了產品,那麼警報用戶

<form action="" method="POST"> 
    Quantity1: <input type="text" name="quantity1" id="quantity1"> 
    Product1: <select name="product1" id="product1"><option value="1">Product 1</option><option value="2">Product 2</option> ... <option value="15">Product 15</option></select> 
    Price1: <input type="text" name="price1" id="price1"> 
    Quantity2: <input type="text" name="quantity2" id="quantity2"> 
    Product2: <select name="product1" id="product2"><option value="1">Product 1</option><option value="2">Product 2</option> ... <option value="15">Product 15</option></select> 
    Price2: <input type="text" name="price2" id="price2"> 
    . 
    . 
    . 
    Quantity15: <input type="text" name="quantity15" id="quantity15"> 
    Product15: <select name="product15" id="product15"><option value="1">Product 1</option><option value="2">Product 2</option> ... <option value="15">Product 15</option></select> 
    Price15: <input type="text" name="price15" id="price15"> 
    <input type="submit" value="submit"> 
</form> 

我想如果產品已被選中,然後提醒,這是選擇的用戶進行檢查。 它應該是這樣的,因爲我想

$('select[id^="product"]').change(function() { 
    //empty array 
    var selected_values = array(); 
    //check value if it is in array then alert 
    if ($(this).val() IS IN ARRAY) { 
     var product = $(this).text(); 
     alert("you have already select this"+product); 
    } else { 
     // else save it in array 
     selected_values[] = $(this).val(); 
    } 
} 

像這樣的事情http://jsfiddle.net/GKTYE/但在這提醒我提出想上選擇更改。請幫忙!

+0

您忘了發佈jQuery代碼... – elclanrs

+0

Javascript部件在哪裏? – djakapm

+0

你想提醒用戶什麼時候?在他們提交之前或之後? – Popnoodles

回答

3

添加警報的方法。

<script type="text/javascript"> 
    alertUser (productName, productSize) { 
     // some method body 
alert("You have selected a product: " + productName + "\r\nSize: " + productSize); 
     } 
    </script> 

在所需的選擇標籤內插入選擇標籤屬性「on change」下的方法提供方法名稱。

<select name="product1" id="product1" onchange="alertUser('product1', 'Large')"> 

這將做什麼是當用戶更改選擇時調用alertUser()方法。您需要編寫一個if語句來確定何時選擇了所需的選項。您可以編寫一個處理多個案例的函數,並通過使用參數來提高腳本編寫速度,從而提高代碼的可用性。

+0

您是否使用過任何其他編程語言,或者您是否剛剛接觸編程(這不包括HTML)?您的回答將使您更容易 – Matt

+0

是的,我知道php和C++,但在javascript中爲零 – Saleem

+0

Sweet。Javascript是一種弱類型語言,它基本上意味着你不必聲明你的變量類型,函數被調用以響應用戶事件。從本質上講,你所要做的就是將函數名放置在用戶正在與之交互的HTML對象的標籤中,這些相互作用包括「onclick」,「onmouseover」等等,例如,如果我想要背景色當用戶鼠標位於div頂部時要更改div,您需要插入一個如下所示的函數: – Matt

相關問題