webber

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            11 Posts :: 2 Stories :: 3 Comments :: 0 Trackbacks
          document.form.action,表單分向提交,javascript提交表單
          同一個表單可以根據用戶的選擇,提交給不同的后臺處理程序。即,表單的分向提交。如,在編寫論壇程序時,如果我們希望實現用戶在發送貼子的時候,既發送提交功能又有預覽功能時,就會遇到上述問題。即,當用戶點擊提交按鈕時,我們希望表單提交給"提交"處理程序;而當用戶點擊預覽按鈕時,我們希望表單提交給"預覽"處理程序。那么,如何實現上述功能呢?下面代碼可以很好的解決這個問題。 
          <form name="form" method="post">
          測試表單:<input name="test"><br>
          <input type="button" value="提交" onClick=send()>
          <input type="button" value="預覽" onClick=preview()>
          </form>
          <script language=javascript>
          function send()
            {
              document.form.action="send.asp"
              document.form.submit()
             }
          function preview()
             {
               document.form.action="preview.asp"
               document.form.submit()
             }
          </script>

                   關于上面實例的兩點說明:
                  1、在整個表單中,不應有名字為action或submit的標簽,否則將會產生"對象不支持此屬性和方法"的錯誤。如代碼 "<input type='xxxx' name='action' >"在表單中是不允許出現的;
                  2、在form標簽中應該存在name屬性。即,應該給表單取一個名字。語句document.form.action和document.form.submit中的"form"也就是表單的名字。
                  表單的分向提交不僅僅使用在論壇的程序中,它還可以運用在許多場合下。恰當的運用表單的分向提交功能可以大大的增強網站的人性化程度。

                  昨天,我調試程序就出現了這樣的問題,就是出現了"對象不支持此屬性和方法"的錯誤,一直無法定位出來,都快瘋掉了,后來在發現一個button命名為submit了。

          1.--------  
             
            <form   name="formname"   action="">  
            <input   name="inputname">  
            <input   type="submit"   onclick="document.formname.action='a.asp'"   value="button   a">  
            <input   type="submit"   onclick="document.formname.action='b.asp'"   value="button   b">  
            <input   type="submit"   onclick="document.formname.action='c.asp'"   value="button   c">  
            </form>      
            2.----------  
            <input   type=button   name="a"   value="a"   onclick="this.form.action='a.asp';this.form.submit();">  
            <input   type=button   name="b"   value="b"   onclick="this.form.action='b.asp';this.form.submit();">  
            <input   type=button   name="c"   value="c"   onclick="this.form.action='c.asp';this.form.submit();">  
            <input   type=button   name="d"   value="d"   onclick="this.form.action='d.asp';this.form.submit();">
          posted on 2010-02-10 16:35 webber 閱讀(2567) 評論(1)  編輯  收藏

          Feedback

          # re: document.form.action[未登錄] 2012-07-12 23:24 allen
          跟樓主犯的錯誤一樣,調了半晚上,差點瘋掉,都準備放棄了看到這篇文章,一試之下還真管用,多謝樓主了  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 虞城县| 台中县| 瑞昌市| 儋州市| 罗源县| 张家口市| 滕州市| 工布江达县| 辽宁省| 惠水县| 新和县| 旬阳县| 永城市| 哈尔滨市| 延庆县| 红河县| 周宁县| 沙河市| 佛山市| 太仆寺旗| 吴堡县| 平湖市| 金塔县| 怀远县| 巴塘县| 武强县| 界首市| 昌宁县| 大化| 建瓯市| 邛崃市| 蛟河市| 嘉鱼县| 筠连县| 高雄市| 新闻| 敦煌市| 丘北县| 呈贡县| 湘乡市| 无为县|