使用 find 函数定位兄弟值

Using the find function to locate a sibling value

我正在尝试为用户单击 "Apply" 的特定作业记录获取 .postingTitle 的值。为此,我尝试使用 jobTitle = $('.applyButton').parents().find('.postingTitle').val(); 来查找 postingTitle.

截至目前,我没有得到任何东西来填充。输出只是我的全局变量 jobTitle = '';。有谁知道为什么我的 find 功能不起作用?

JS中注释掉的代码是我试过的其他方法

JS

var jobTitle = '';
$('.applyButton').on('click', function (event) {
    //jobTitle = $('.applyButton').siblings().find('.postingTitle').val();
    jobTitle = $('.applyButton').parents().find('.postingTitle').val();
    //jobTitle = $(this).closest('.postingTitle').val();
    $('#jobTitle').html(jobTitle);
    console.log(jobTitle);
});

PHP

foreach ($career_rows as $career_row) {
        $category = $career_row['category'];
        $job_title = $career_row['job_title'];
        $job_description = $career_row['job_description'];
        $job_requirements = $career_row['job_requirements'];
        $job_status = $career_row['active'];
            echo '<div class="posting">';
                echo '<div class="postingInner">';
                    echo '<span class="postingCategory hGc">'. $category .'</span>';
                    echo '<h3 class="postingTitle">'. $job_title .'</h3>';
                    echo '<p class="postingDescription dGsmall">'. $job_description .'</p>';
                    echo '<p class="dG bold jobRequirementsTitle">Requirements:</p>';
                    //echo '<ul class="jobRequirementList">'. $job_requirements .'</ul>';
                    echo '<ul class="jobRequirementList">'. join('</li><li class="dGsmall">',preg_split('/@@@/',$job_requirements)) .'</ul>';
                    echo '<div class="applyButtonWrap"><button class="buttonInv2 applyButton">Apply</button></div>';
                echo '</div>';
            echo '</div>';
    }

.val() 仅适用于 inputselecttextarea 以及其他一些人。如果你想获得另一个元素内的文本,你会使用 .text(),因为 .postingTitle 是一个 h3,你会想做:

 jobTitle = $(this).closest('.postingInner').find('.postingTitle').text();