【问题标题】:Wordpress search form actionWordpress 搜索表单操作
【发布时间】:2017-03-21 04:41:47
【问题描述】:

我需要在我的 wordpress 页面中执行自定义搜索功能,需要将一些 javascript 代码放入标题中并使用 wordpress 搜索表单来调用该功能。

在wordpress的主题文件夹下,我在header.php的tag之间添加了一些javascript代码

<head>
  <script>
      var test123456 = function() {
            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        }

     </script>

</head>

默认主题 searchform.php 是:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form> 

请问如何通过searchform.php调用那个javascript函数?

我的表单部分:

<form action="javascript:test123456()">
    <input type="text" name="firstname" value="" id="curlinput">
    <input type="submit" value="Submit">
</form> 

非常感谢和赞赏!!

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:

    把你的代码改成

    <form onsubmit="test123456()">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
    

    【讨论】:

    • 非常感谢 aje,我需要删除 action="" ,对吗?
    • 是的,并且在提交时也会调用 JavaScript 函数。
    • 谢谢 aje ~~ 感激不尽!
    • 对不起,我还有一个问题是............当点击“搜索按钮”时,这将直接在当前页面(不是搜索页面)上运行 javascript 函数,即意味着它不会重定向到 search.php 。那么,如果我想保留默认表单操作( action="" )并在 search.php 上运行 javascript 函数,我该怎么办?非常感谢~~~
    • 在这种情况下,在页面加载时调用您的函数,例如 $(document).ready(function(){ YOUR_FUNCTION() }) in search.php
    【解决方案2】:

    你可以把表格放在这行。

    <form role="search" method="get" id="searchform" action="javascript:void(0);" onsubmit="test123456();">
        <div><label class="screen-reader-text" for="s">Search for:</label>
            <input type="text" value="" name="s" id="s" />
            <input type="submit" id="searchsubmit" value="Search" />
        </div>
    </form>
    

    或者像下面这样

    <form role="search" method="get" id="searchform" action="javascript:test123456()">
        <div><label class="screen-reader-text" for="s">Search for:</label>
            <input type="text" value="" name="s" id="s" />
            <input type="submit" id="searchsubmit" value="Search" />
        </div>
    </form>
    

    如果您还想设置动作文件,请尝试以下代码。

    <form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>" onsubmit="test123456();">
            <div><label class="screen-reader-text" for="s">Search for:</label>
                <input type="text" value="" name="s" id="s" />
                <input type="submit" id="searchsubmit" value="Search" />
            </div>
        </form>
    

    【讨论】:

    • @lingmak 欢迎您...如果我的回答对您有用,请给我投票。
    • 对不起,我还有一个问题是............当点击“搜索按钮”时,这将直接在当前页面(不是搜索页面)上运行 javascript 函数,那就是意味着它不会重定向到 search.php 。那么,如果我想保留默认表单操作( action="" )并在 search.php 上运行 javascript 函数,我该怎么办?非常感谢~~~
    • 嗨 Jalpa,...我在这里用我的代码发布了问题:stackoverflow.com/questions/43083669/…
    • @lingmak 请查看对您的新问题的评论。
    猜你喜欢
    • 1970-01-01
    • 2019-05-21
    • 2014-10-01
    • 2016-12-18
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    相关资源
    最近更新 更多