2011-02-28 133 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