首页 > 代码库 > 将提交按钮变成链接

将提交按钮变成链接

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example:Submit buttons to links</title>
  <style type="text/css"></style>
  <script type="text/javascript" src=http://www.mamicode.com/"submitToLinks.js"></script>
</head>
<body>
  <form action="nogo.php" method="post">
  <p>
   <label for="Name">Name:</label>
   <input type="text" id="Name" name="Name"/>
   <input type="submit" value=http://www.mamicode.com/"send"/>
  </p>
  <p>
   <label for="Email">Email:</label>
   <input type="text" id="Email" name="Email"/>
   <input type="submit" value=http://www.mamicode.com/"check"/>
  </p>
  </form>
</body>
</html>

 

submitToLinks.js

function submitToLinks()
{
    if(!document.getElementById||!document.createTextNode){return;}
    var inputs,i,newLink,newText;
    inputs=document.getElementsByTagName(‘input‘);
    for(i=0;i<inputs.length;i++)
    {
        if(inputs[i].getAttribute(‘type‘).toLowerCase()!=‘submit‘)
        {continue;i++}
        newLink=document.createElement(‘a‘);
        newText=document.createTextNode(inputs[i].getAttribute(‘value‘));
        newLink.appendChild(newText);
        newLink.setAttribute(‘href‘,‘javascript:document.forms[0].submit()‘);
        inputs[i].parentNode.replaceChild(newLink,inputs[i]);
        //if(i<inputs.length){i--};
    }
}
window.onload=submitToLinks;

 

image

 

image

将提交按钮变成链接