代码之家  ›  专栏  ›  技术社区  ›  Josh K

jquery操作表单

  •  -2
  • Josh K  · 技术社区  · 15 年前

    出于某种原因,这并没有改变论坛的行动。我有一些代码可以在单击按钮时更改窗体的操作:

        function changeForm(event){
        alert("Before: "+jQuery("#franchiseform").attr("action"));
        jQuery("#franchiseform").attr("action", "franchisepreview.php");
       alert("After: "+jQuery("#franchiseform").attr("action"));
        jQuery("#franchiseform").submit();
        }
    

    装订:

        jQuery("input.preview").bind("click", changeForm);
    

    形式:

    <form method="post" enctype="multipart/form-data" action="franchiseinsert.php" class="insert-new" id="franchiseform">
    

    按钮:

    <input type="button" value="Preview" id="preview" name="preview" id="preview" class="preview" /><input type="submit" value="Insert" />
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   ryanulit    15 年前

    首先,你需要清理

    <input type="button" value="Preview" id="preview" name="preview" id="preview" class="preview" />
    

    到:

    <input type="button" value="Preview" id="preview" name="preview" />
    

    作为输入不应具有多个ID属性,也不应使用具有相同名称的类和ID。这可能是你有问题的原因之一。然后我使用了以下代码,操作URL实际上正在更改:

    <html>
    <head>
        <title></title>
    
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    
        <script type="text/javascript">
            $(document).ready(function() {
    
                $("#preview").bind("click", changeForm);
    
                function changeForm(event){
                    alert("Before: "+ $("#franchiseform").attr("action"));
                    $("#franchiseform").attr("action", "franchisepreview.php");
                    alert("After: "+ $("#franchiseform").attr("action"));
                    $("#franchiseform").submit();
                }   
            });
        </script>
    
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" action="franchiseinsert.php" class="insert-new" id="franchiseform">
            <input type="button" value="Preview" id="preview" name="preview" />
            <input type="submit" value="Insert" />
        </form>
    </body>
    </html>
    
        2
  •  0
  •   Cryophallion    15 年前

    除非去掉了太多内容,否则看起来您需要将“after”调用放入名为“changeform”的函数中,该函数正是您绑定到click事件的函数。

    否则,请继续链接:

    jQuery("input.preview").bind("click", function(){
        jQuery("#franchiseform").attr("action", "franchisepreview.php"));
        jQuery("#franchiseform").submit();
    });
    
        3
  •  0
  •   Josh K    15 年前

    事实证明,这是一个特定于Firefox/Mac的bug。它可以在其他平台和浏览器上工作,但由于某种原因,不能在这个特定的计算机上工作。可能是扩展冲突或其他问题,我只需要使用其他浏览器。