简体   繁体   中英

Javascript form validation does not link radio buttons to links

I have created 2 function to validate password and link to pages using radio buttons, but one of the radio button does not link me to anything

Function to check password

function check()
{
  var password=document.getElementById('password').value;
  var confirm=document.getElementById('confirm').value;

  if(password==confirm)
  {
    var form=document.getElementById('form');
    return true;
   }

  else
  {
     alert('Password do not match');
     return false;
   }


}

Function to validate radio buttons

function doSubmit(form) 
{
    var urls = form['url'];
    var i = urls && urls.length;
    var k=false;
    var k=check();
    while(k!=false)
    {
       var urls = form['url'];
       var i = urls && urls.length;
       while (i--) 
       {
          if (urls[i].checked) 
           {
            console.log(urls[i].value);
            window.location = urls[i].value;
           }
          else
           {
             return false;
           }
      }
    }
return false;
}

HTML code, The radio button Beginner is not linking me to the webpage, any suggestion on how to fix it??

<form action="" onsubmit="return doSubmit(this)">

    <label for="password">Password</label>
       <input type="password" id="password" maxlength="20" size="10"   required="true" pattern=".{7,}" title="Password should be atleast 7 characters long"/>

 <br /><br />

    <label for="confirm">Confirm Password</label>
       <input type="password" id="confirm"" maxlength="20" size="10" required="true"   />

  <br /><br/>

     <label for="mcq">MCQ</label>
      Beginner<input type="radio" name="url"  value="tyrano.html"required="true"/>
                <br /> 
      Expert<input type="radio" name="url" value="spino.html"required="true"/>
                <br /><br />

<button type="reset"  id="reset">Reset</button>     
<button type="submit" id="submit">Submit</button>
</form>

To redirect to other page, javascript has location.href to do your work.

document.getElementById("id_of_your_radio").onclick = function() {
    location.href = "you_redirected_url";
}

Try and see it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM