2017-06-21 42 views
1

在選擇框值的語句我有一個指向選擇框PHP頁面上寫在JavaScript中function問題。這if語句是指選擇框如果在一個javascript函數

if (!(document.getElementById("add_product").value)==="Choose here") 

下面的代碼:

     function promptEnrollmentMonth() { 
         if (!(document.getElementById("add_product").value)==="Choose here") { 
          var month = prompt("Is this the correct month for this course/product?", "<?php echo "$EnrollmentMonth"; ?>"); 
          if (month !=null) { 

          } 
         } 
        } 

<button type="submit" 
onclick="promptEnrollmentMonth()">Update</button> 

<div>Add course/product:<select name='add_product'> 
<option selected>Choose here</option> 
<option>Other options...</option> 
</select></div> 
+1

如果(的document.getElementById( 「add_product」)。值!== 「這裏選擇」) – Conceptz

回答

1

你在if語句

if (!(document.getElementById("add_product").value)==="Choose here") 

應該

if (document.getElementById("add_product").value !=="Choose here") 

需要設置在這裏選擇的初始選項的值一個錯字。或者

if (document.getElementById("add_product").value !=="") 

如果您不想將值設置爲初始選擇選項,則此情況適用。

!(document.getElementById("add_product").value) 

只會轉化爲真或假,這將永遠不會等於 「這裏選擇」

3

第一個問題是與"<?php echo "$EnrollmentMonth"; ?>"

它應該是這樣的:

var month = prompt("Is this the correct month for this course/product?", "<?php echo $EnrollmentMonth; ?>"); 

<option selected>Choose here</option>你還沒有到選項標籤提供的價值屬性:

<option value='Choose here' selected>Choose here</option> 
相關問題