【问题标题】:Jquery - Select the div within the same container class div as this?Jquery - 在与此相同的容器类 div 中选择 div?
【发布时间】:2012-06-13 16:13:07
【问题描述】:

我在 foreach 循环中循环浏览数据。在每个循环中,一个容器类包含以下内容:

foreach($resultarray AS $value){
        $filename = substr($value['img_file_name'],9);
        $cat_id = $value['cat_id'];
        echo '<article class="post">';
        echo '<div class="post_title">' . $value['post_title'] . '</div>';
        echo '<div class="post_info">' . 
        'Category: ' . $cat_name = get_cat_name($cat_id) .'<br />'. 
        'Year: ' . $value['post_year'] .'<br />'. 
        $value['post_desc'] .'<br />'. 
        '</div>';
        echo '<div class="link-to-post"><a href="#">Click to view</a></div>';
        echo '<a href="#" class="linktopost"><img class="post-thumb" src="img/thumb_/'.$filename.'" alt="MJbox Michael Jackson memorabilia thumbnail" /></a>';
        echo '<a href="#" class="linktopost"><img class="cover-img" src="img/post-bg-1.png" alt="test" /></a>';
        echo '<form name="form"><input type="text" class="postid" value="'.$value['post_id'].'" /></form>';
        echo '</article>';

    }

当单击“lintopost”链接/图像时,我使用 Jquery 使以下 div 可见。

<div id="main-post">
        <div id="gotpostid">some text</div>
</div>

目前我正在使用 collowing Jquery 来处理#main-post div 何时出现以及何时获取该帖子的数据以放入 div 中。

$(".cover-img").click(function(){
        $("#main-post").fadeIn(1000);
        $.post("inc/fullpost.php", {postid: $('.postid').val()},
            function(output){
                $("#gotpostid").html(output).show();
            }).fail(function(x,y,z){ 
                $("#gotpostid").html(x + "<br />" + y + "<br />" + z)
            });

});

我目前遇到的问题是输入“postid”的值没有被正确选择。我如何选择它,以便它从 this 输入字段中获取值,而不仅仅是从它遇到的第一个 .postid 中获取值,这使得每次迭代的 id 都相同。

我希望我能很好地解释自己。

【问题讨论】:

    标签: javascript jquery ajax jquery-selectors


    【解决方案1】:

    1) 我建议您在要在标记上添加新元素时使用.on('click', function() {

    2) 点击图片后,使用.find().parent() 获得准确的输入。

    【讨论】:

    • 嗨,我试过{postid: $(this).parent().find('input[class="postid"]').val()}。哪个似乎正确使用 parent 后退到结构中一次以查找具有该名称的输入,但它似乎不起作用?
    • 尝试 input.postid,而不是 [class="postid"],所以:.find('input.postid').val()
    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多