JS动态改变form表单里的action值属性的方法

来源:互联网 发布:推女郎吧最新域名 编辑:程序博客网 时间:2024/05/01 10:58

前几天自己看了一个小程序,对form里的action值有所困惑,后来才明白其原理是动态改变form表单里的action值。这里主要介绍两种方法。

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>第一种方法</title><script language="javascript">    function check() {        if (document.form1.a[0].checked == true)<!--表示选中页面一-->            document.form1.action = "1.jsp"        else            document.form1.action = "2.jsp"    }</script></head><body>    <form name="form1" method="post" action="first" onSubmit="check();">        页面一<input type="radio" name="a">         页面二<input type="radio" name="a">         <input name="" type="submit" value="提交">    </form></body></html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>第二种方法</title></head><body>    <form id="form1" name="form1" method="post" action="../news/index.asp">        <table width="100%" height="43" border="0" cellpadding="0"            cellspacing="0">            <tr>                <td height="28"><input name="keyword" type="text"                    style="width: 150px" id="keyword" /></td>            </tr>            <tr>                <td height="28">                <select name="Searchtype" style="width: 110px" id="Searchtype" onchange="Searchtype1();">                        <option value="news" selected="selected">新闻中心</option>                        <option value="case">工程案例</option>                </select>                 <input type="submit" name="Submit" value="搜索" />                </td>            </tr>        </table>    </form>    <script language="javascript">  function Searchtype1(){      <!--注意以下使用方法-->  var type=document.getElementById("Searchtype").options[document.getElementById("Searchtype").selectedIndex].value;  if (type=="news"){      document.getElementById("form1").action="../news/index.asp"      }  else if (type=="case"){      document.getElementById("form1").action="../case/index.asp"      }  }  </script></body></html>
0 0
原创粉丝点击