2011-02-28 64 views
0

如何添加在線驗證,以確保無線輸入的選擇,必須選擇添加jQuery的表單驗證Javascript?

<script type="text/javascript"> 
    function choosePage() { 
     if(document.getElementById('weightloss').form1_option1.checked) { 
      window.location.replace("http://google.com/"); 
     } 
     if(document.getElementById('weightloss').form1_option2.checked) { 
      window.location.replace("http://yahoo.com/"); 
     } 
    } 
</script> 

<form id="weightloss"> 
    <input type="radio" id="form1_option1" name="weight-loss" value="5_day" class="plan"> 
    <label for="form1_option1"> 5 Day - All Inclusive Price</label><br> 
    <input type="radio" id="form1_option2" name="weight-loss" value="7_day"> 
    <label for="form1_option2"> 7 Day - All Inclusive Price</label><br> 
    <input type="button" value="Place Order" alt="Submit button" class="orange_btn" onclick="choosePage()"> 
</form> 
+0

編輯職位,包括一個代碼塊,這樣所有的標籤出現... – LorenVS 2011-02-28 22:16:28

+0

爲什麼你不選擇默認的收音機之一? – m1k3y3 2011-02-28 22:23:51

回答

0

您使用document.getElementById('weightloss')但INFACT沒有具有ID 'weightloss'任何元素。

嘗試用這種

<script type="text/javascript"> 
    function choosePage() { 
     if(document.getElementById('form1_option1').checked) { 
      window.location.replace("http://google.com/"); 
     } 
     if(document.getElementById('form1_option2').checked) { 
      window.location.replace("http://yahoo.com/"); 
     } 
    } 
</script> 

希望它會幫助你。

+1

weightloss是表格上的ID – user634599 2011-02-28 22:24:06

+0

嘗試使用單選鍵ID – GitsD 2011-02-28 22:26:02

+0

我想要的是進一步添加驗證到我上面的代碼..它完美的作品...做我想做的事......如果我選擇一個選擇它去谷歌..如果另一個去yahoo.com..thats perect ..但我想要的是,如果用戶嘗試之前提交...它應該提示他們選擇一個。 – user634599 2011-02-28 22:29:01

0

你只需要讓js先評估表單。

<script type="text/javascript"> 
    function choosePage() { 
     if(document.forms["weightloss"]["form1_option1"].checked == true) { 
      window.location.replace("http://google.com/"); 
     } 
     if(document.forms["weightloss"]["form1_option2"].checked == true) { 
      window.location.replace("http://yahoo.com/"); 
     } 
     else { alert('Please choose an option'); } // for inline alerts see below. 
    } 
</script> 

對嵌入式文本提示請嘗試以下操作:

else { my_message.innerHTML = "Please choose an option" } 

的元素添加到您的網頁包含消息:

<p id="my_message"></p> <!-- when the form submits empty your message will appear here. 
+0

現在強制選擇表單提交 – Dawson 2011-02-28 22:31:44

+0

這是完美的。只要做出提示內聯html文本? – user634599 2011-02-28 22:35:42

+0

嗨道森如果我有20個問題呢? – user634599 2011-02-28 22:58:58