【问题标题】:submit form with link while checking values在检查值时提交带有链接的表单
【发布时间】:2009-11-06 02:56:23
【问题描述】:

我有一个 HTML 表单定义为

<form name="writeYourAd" id="writeYourAd" method="post" action="post.php?action=preview" onsubmit="return checkContentForm(this);" enctype="multipart/form-data">

如果我使用 a ,它可以正常工作。但如果我改用这个:

<a href="#" class="mmh_orngebtn mmh_grybtn" onclick="javascript:document.writeYourAd.submit();"><span>continue</span></a>

它提交表单但不运行 checkContentForm 函数(或者至少它让我通过反正)。

为什么?我能做什么?

【问题讨论】:

    标签: javascript html forms


    【解决方案1】:

    我认为您需要将 onclick 处理程序指向您的 checkContentForm 函数。

    或者编写自己的提交函数,包括调用 checkContentForm 然后提交。

    【讨论】:

      【解决方案2】:

      尝试将函数作为 onclick 事件添加到提交按钮

      【讨论】:

      • 确保在弹出任何错误时返回 false 以便它不会提交
      【解决方案3】:

      您应该手动调用form.onsubmit(),然后将其删除(以防某些浏览器会在submit() 调用时调用它,因为这是他们应该按照规范执行的操作,但大多数不这样做),然后调用表单.submit()

      也可以看看this thread

      【讨论】:

        【解决方案4】:

        它会在执行 onsubmit 功能之前转到您的 post.php 页面

        尝试停留在同一页面并在 post.php 中使用包含

        喜欢

        if ($_POST[submit]){
         include "post.php";
         echo "<script>return checkContentForm(document.writeYourAd);</script>";
        }
        

        或者您可以删除该操作,然后使用 ajax 在 checkContentForm() 函数之前或之后发送您的帖子

        <form name="writeYourAd" id="writeYourAd" method="post" action="" onsubmit="return SendQuery(this);" enctype="multipart/form-data">
        

        JavaScript:

        function SendQuery(myForm)
        {
         xmlhttp=GetXmlHttpObject();
        if (xmlhttp==null)
          {
          alert ("Browser does not support HTTP Request");
          return;
          }
        
        var url="post.php";
        url=url+"?action=preview";
        thisForm = myForm;
        xmlhttp.onreadystatechange=stateChanged;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
        }
        
        function stateChanged(){
            if (xmlhttp.readyState==4){
                if (xmlhttp.status == 200){
                    return checkContentForm(thisForm);
                }
            }
        }
        
        function GetXmlHttpObject()
        {
        if (window.XMLHttpRequest)
          {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          return new XMLHttpRequest();
          }
        if (window.ActiveXObject)
          {
          // code for IE6, IE5
          return new ActiveXObject("Microsoft.XMLHTTP");
          }
        return null;
        }
        

        只是检查一些错误。

        【讨论】:

          猜你喜欢
          • 2011-06-15
          • 1970-01-01
          • 1970-01-01
          • 2019-11-08
          • 1970-01-01
          • 1970-01-01
          • 2010-10-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多