简体   繁体   English

POST循环内的jQuery onclick函数错误

[英]JQuery onclick function error within post loop

I have a loop of posts in wordpress and I am trying to call a jquery on click show event which will show a contact form for that post when clicked. 我在wordpress中有一个帖子循环,我试图在click show事件上调用一个jquery,它将在单击时显示该帖子的联系表单。

The onclick show works only for the first post in the loop where it will display the contact form for that post no problem - but, it does not work for any of the other posts in the loop. onclick展示仅适用于循环中的第一条帖子,在该帖子中它将毫无问题地显示该帖子的联系表单-但不适用于循环中的任何其他帖子。 Anyone know why this may be? 有人知道为什么会这样吗? The page with the post loop is https://www.salusa.co.uk/specialist-training-courses/ . 带有发布循环的页面是https://www.salusa.co.uk/specialist-training-courses/

The code is roughly: 代码大致如下:

<div class="archive-posts-loop">

<div class="post">
    <div class="enquire">               
        <a class="show-form">Contact Provider</a>                   
    </div>

<div class="contact-form-wrapper"  id="contact-form-wrapper" style="display:none;"> 
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>

$(document).ready(
function(){

    $(".show-form").click(function () {
        $("#contact-form-wrapper").show("fast");
    });   
    $("#close").click(function () {
        $("#contact-form-wrapper").hide("fast");
    });
});

</script>


</div><!--post-->


<div class="post">
    <div class="enquire">               
        <a class="show-form">Contact Provider</a>                   
    </div>

<div class="contact-form-wrapper"  id="contact-form-wrapper" style="display:none;"> 
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>

$(document).ready(
function(){

    $(".show-form").click(function () {
        $("#contact-form-wrapper").show("fast");
    });   
    $("#close").click(function () {
        $("#contact-form-wrapper").hide("fast");
    });
});

</script>

</div><!--post-->

</div><!--archive-post-loop-->

As you have a new form for each post you need to target the correct form for the post. 由于每个帖子都有一个新表单,因此需要针对该帖子指定正确的表单。 You can listen for delegated click events and show/hide the nested form accordingly. 您可以侦听委托的单击事件,并相应地显示/隐藏嵌套表单。 Note that you do not need (nor should have) the script repeated for each form as shown in your example. 请注意,您不需要(也不应该)为示例中所示的每种表单重复脚本。

 $(function(){ $(".post") .on("click", ".show-form", function() { $(this).parents(".post").find(".contact-form-wrapper").show("fast"); }) .on("click", ".close", function() { $(this).parent().hide("fast"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="post"> <div class="enquire"> <a class="show-form">Contact Provider</a> </div> <div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;"> [Form here] <a href="#" class="close">close</a> </div> </div> <div class="post"> <div class="enquire"> <a class="show-form">Contact Provider</a> </div> <div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;"> [Form here] <a href="#" class="close">close</a> </div> </div> <div class="post"> <div class="enquire"> <a class="show-form">Contact Provider</a> </div> <div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;"> [Form here] <a href="#" class="close">close</a> </div> </div> 

You are using multiple elements with the same id of contact-form-wrapper . 您正在使用具有相同id的contact-form-wrapper多个元素。 Instead you need to make each element have a unique id, and then select that id in the respective jquery calls. 相反,您需要使每个元素具有唯一的ID,然后在相应的jquery调用中选择该ID。 Jquery will return the first matching element given a selector, which is why it only selects the first post each time. jQuery将返回给定选择器的第一个匹配元素,这就是为什么它每次仅选择第一个帖子的原因。

Here is a codepen that does what you want. 这是一个可满足您需求的代码笔。

https://codepen.io/abidhasan/pen/wpdoyO?editors=1111 https://codepen.io/abidhasan/pen/wpdoyO?editors=1111

This is the jQuery that you can use - basically look for the sibling and change its style 这是您可以使用的jQuery-从根本上寻找同级并更改其样式

$(".post .show-form").on("click", function() {
  $(this).parent().siblings()[0].style.display= "block";
});

$(".post .hide-form").on("click", function() {
  $(this).parent().siblings()[0].style.display= "none";
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM