无法更改jQuery按钮提交以下拉选择并提交

时间:2018-09-19 11:03:14

标签: javascript php jquery wordpress forms

我有一个wp_query,我想通过下拉选择框更改每页的帖子。我已经从各种教程和摘要中拼凑出的代码让我忍不住...

我的基本wp_query是;

<?php

$postsperpage = isset($_POST['amt_per'])? $_POST['amt_per']: 10;


        $args = array(
        'post_type'=>'product',
        'posts_per_page' => $postsperpage,
    );

    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';

        wp_reset_postdata();

    } else {

    }

?>

我使用的教程有几个按钮,其中的值是每页的帖子数;

    <form method="post">
  <select id="select-box">
    <option class="amt-button" name="amt_per" value="1">1</option>
    <option class="amt-button" name="amt_per" value="2">2</option>
    <option class="amt-button" name="amt_per" value="3">3</option>
    <option class="amt-button" name="amt_per" value="4">4</option>
  </select>
</form>

和jQuery;

<script>
jQuery(document).ready(function($) {
    // Postsperpage
    $('select').click(function(){
        $(this).closest('form').submit();
    });
});
</script>

我想要做的就是将按钮更改为这样的下拉选择框;

<select id="select-box" method="post">
    <option class="amt-button" name="amt_per" value="1">1</option>
    <option class="amt-button" name="amt_per" value="2">2</option>
    <option class="amt-button" name="amt_per" value="3">3</option>
    <option class="amt-button" name="amt_per" value="4">4</option>
</select>

我现在遇到的问题是我修订的jQuery不会使用正确的每页帖子数更新查询,这就是我想出的内容;

$(function() {
  $('#select-box').on('change', function(e) {
    $(this).closest('form')
           .trigger('submit')
  })
})

感谢所有帮助,谢谢!

UPDATE

由于@KrystianBarchański,我已经更新了我的下拉代码并将包裹在。当您选择一个下拉值时,这种形式就触发了,但是现在页面仅以每页相同的帖子重新加载,并且下拉菜单变回初始值,我假设这是一个jQuery问题,因为使用按钮时一切正常?

2 个答案:

答案 0 :(得分:1)

您必须像这样用<select>包裹<form>

<form  method="post">
  <select id="select-box">
    <option class="amt-button" name="amt_per" value="1">1</option>
    <option class="amt-button" name="amt_per" value="2">2</option>
    <option class="amt-button" name="amt_per" value="3">3</option>
    <option class="amt-button" name="amt_per" value="4">4</option>
  </select>
</form>

在您的示例中,您将<select>视为可以提交并且会发送发帖请求,但这只能通过表单来完成。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select-在这里您可以找到可用属性列表,以选择没有methodurl的情况。

答案 1 :(得分:1)

您需要更改选择框名称library(knitr) library(kableExtra) dt <- mtcars[1:5, 1:4] # LaTeX table a <- kable(dt, format = "latex", caption = "Demo Table") %>% kable_styling(bootstrap_options = "striped", full_width = F) %>% add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>% add_footnote(c("table footnote")) filename <- tempfile(fileext = ".tex") capture.output(a, file = filename) foo <- readLines(filename) writeLines(c("\\documentclass[hidelinks]{article}", "\\begin{document}", foo, "\\end{document}"), con = filename) tools::texi2dvi(filename, pdf = TRUE) 。不是选项。

amt_per