如果所选值更改而没有按钮,则提交 WordPress 表单

发布于 2024-11-15 14:19:22 字数 1076 浏览 1 评论 0原文

我正在尝试提交没有提交按钮的表单。 此表单从 WordPress 中的自定义字段获取值,并在另一个页面中显示结果。我一直在尝试使用通常的 javascript 函数 onchange 但没有任何反应,我也尝试过使用函数...有谁知道这有什么问题或者是否有更好的方法来解决它?是不是和Wordpress或者post方法有关?这是表单的代码:

<form method="get" name="form" action="/busqueda/" >
        <div class="search">

            <select class="styled" name="author"  onChange="this.form.submit();">
            <option value="-1" selected>Authors</option>    
            <?php
            $metakey = 'author';
            $autores = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
            if ($authors) {
            foreach ($authors as $author) {
            echo "<option VALUE=\"" . $author . "\">" . $author . "</option>";
                                    }
                            }
                ?>
            </select>
        </div><!-- #search1 -->

</form>

顺便说一句,当使用标准提交按钮显示结果时,该表单工作正常。 欢迎任何可能出错的提示。多谢!

I'm trying to submit a form without a submit button.
This form takes values from custom fields in wordpress, displaying results in another page. I've been trying with the usual javascript function onchange and nothing happens, I've tried with functions as well...does anyone know what's wrong with this or if is there a better way to solve it? Can it be something related to Wordpress, or the post method? This is the code for the form:

<form method="get" name="form" action="/busqueda/" >
        <div class="search">

            <select class="styled" name="author"  onChange="this.form.submit();">
            <option value="-1" selected>Authors</option>    
            <?php
            $metakey = 'author';
            $autores = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
            if ($authors) {
            foreach ($authors as $author) {
            echo "<option VALUE=\"" . $author . "\">" . $author . "</option>";
                                    }
                            }
                ?>
            </select>
        </div><!-- #search1 -->

</form>

BTW, the form works fine when displaying results with a standard submit button.
Any hint of what can be wrong is welcome. Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-11-22 14:19:23

我希望您能够通过 $_GET 正确检索发布后的值,因为当我在浏览器中运行代码时,该值在 URL 中很好地传递。检查您的检索代码,确保您使用的是 $_GET 而不是 $_POST。

I hope you are retrieving the value after the post correctly through $_GET because when i ran the code in my browser, the value is passing on in the URL quite nicely. Check your retrieval code make sure you are using $_GET and not $_POST.

殤城〤 2024-11-22 14:19:22

试试这个:

<form method="get" name="form" action="/busqueda/" >
        <div class="search">

            <select class="styled" name="author" onChange="document.forms['form'].submit();">

            <?php
            $metakey = 'author';
            $autores = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
            if ($authors) {
            foreach ($authors as $author) {
            echo "<option VALUE=\"" . $author . "\">" . $author . "</option>";
                                    }
                            }
                ?>
            </select>
        </div><!-- #search1 -->

</form>

用 jQuery 提交更新

更新的表单:

<form id="authorForm" method="get" name="form" action="/busqueda/" >
        <div class="search">

            <select id="author" class="styled" name="author">
                <option value="default">Choose an author...</option>
                <?php
                $metakey = 'author';
                $autores = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
                if ($authors){
                    foreach ($authors as $author) {
                        echo '<option value="' . $author . '">' . $author . '</option>';
                    }
                }
                ?>
            </select>
        </div><!-- #search1 -->

</form>

jQuery 脚本更改事件处理和表单提交:

<script type="text/javascript">
$(document).ready(function(){
    $('#author').change(function(){
        if($(this).val() !== 'default'){
            $('#authorForm').submit();
        }
    }); 
});
</script>

您可以从提交测试 jQuery下拉框更改事件

Try this one:

<form method="get" name="form" action="/busqueda/" >
        <div class="search">

            <select class="styled" name="author" onChange="document.forms['form'].submit();">

            <?php
            $metakey = 'author';
            $autores = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
            if ($authors) {
            foreach ($authors as $author) {
            echo "<option VALUE=\"" . $author . "\">" . $author . "</option>";
                                    }
                            }
                ?>
            </select>
        </div><!-- #search1 -->

</form>

Updated with jQuery submit

The updated form:

<form id="authorForm" method="get" name="form" action="/busqueda/" >
        <div class="search">

            <select id="author" class="styled" name="author">
                <option value="default">Choose an author...</option>
                <?php
                $metakey = 'author';
                $autores = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
                if ($authors){
                    foreach ($authors as $author) {
                        echo '<option value="' . $author . '">' . $author . '</option>';
                    }
                }
                ?>
            </select>
        </div><!-- #search1 -->

</form>

jQuery script change event handling and form submit:

<script type="text/javascript">
$(document).ready(function(){
    $('#author').change(function(){
        if($(this).val() !== 'default'){
            $('#authorForm').submit();
        }
    }); 
});
</script>

You can test the jQuery from submit on drop box change event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文