使用jQuery在模板文字中隐藏div时遇到问题。

huangapple go评论45阅读模式
英文:

Trouble hiding a div within a template literal using jQuery

问题

我已经写了这段jQuery代码在Oxygen Builder的JavaScript元素中,用于查询工作板API并返回部门及其工作的数组。我正在测试是否department[0].jobs.length返回0,然后隐藏#job-list div,否则显示它及其相关工作。代码成功查询API并返回0个工作,但三元运算符的其余部分将不会隐藏div。

jQuery(document).ready(function($) {
  $.getJSON('https://boards-api.greenhouse.io/v1/boards/forwardnetworks/departments', postings => {

    $("#div_block-420-61456").html(`
      <div id="job-list">${postings.departments[0].jobs.length == 0 ? $("#job-list").hide() : $("#job-list").show()}<h3 class="dept">${postings.departments[0].name}</h3>
      ${postings.departments[0].jobs.map(item => `<a href="${item.absolute_url}"><h4 class="job-title">${item.title}</h4></a>
      <p class="job-descrip">${item.location.name}`).join('')}</div> `);
  });
});

我通常会得到[object object]的返回值。

英文:

I've written this bit of jQuery code in Oxygen Builder's JavaScript element to query the job board API and return an array of departments and their jobs. I'm testing to see if the department[0].jobs.length returns 0 then hide the #job-list div, otherwise show it and its associated jobs. The code succeeds in querying the API and returning 0 jobs but the remainder of the ternary operator will not hide the div.

jQuery(document).ready(function($) {
  $.getJSON(&#39;https://boards-api.greenhouse.io/v1/boards/forwardnetworks/departments&#39;, postings =&gt; {

    $(&quot;#div_block-420-61456&quot;).html(`
    &lt;div id=&quot;job-list&quot;&gt;${postings.departments[0].jobs.length == 0 ? $(&quot;#job-list&quot;).hide() : $(&quot;#job-list&quot;).show()}&lt;h3 class=&quot;dept&quot;&gt;${postings.departments[0].name}&lt;/h3&gt;
	${postings.departments[0].jobs.map(item =&gt; `&lt;a href=&quot;${item.absolute_url}&quot;&gt;&lt;h4 class=&quot;job-title&quot;&gt;${item.title}&lt;/h4&gt;&lt;/a&gt;
	&lt;p class=&quot;job-descrip&quot;&gt;${item.location.name}`).join(&#39;&#39;)}&lt;/div&gt; `);
  });
});

I generally get a return of [object object]

答案1

得分: 0

如我在评论中提到的,我会在.getJSON成功处理程序内部添加一个守卫条件,如果没有要显示的工作,就提前返回。

最终的函数如下所示:

const departmentIndex = 0;

$(function ($) {
  $.getJSON('https://boards-api.greenhouse.io/v1/boards/forwardnetworks/departments', postings => {
    if (postings.departments[departmentIndex].jobs.length === 0) { return; }

    $("#div_block-420-61456").html(`
      <div id="job-list">
      	<h3 class="dept">${postings.departments[departmentIndex].name}</h3>
      	${postings.departments[departmentIndex].jobs.map(item => `
      		<a href="${item.absolute_url}">
      			<h4 class="job-title">${item.title}</h4>
      		</a>
      		<p class="job-descrip">${item.location.name}`
        ).join('')}
      </div>
    `);
  }); 
});

注意:我将索引放入变量中,以便轻松测试不同的部门。

这里 是一个示例fiddle。

英文:

As I mentioned in the comments, I would add a guard within the .getJSON success handler that will return early if there are no jobs to display.

The resulting function would be:

const departmentIndex = 0;

$(function ($) {
  $.getJSON(&#39;https://boards-api.greenhouse.io/v1/boards/forwardnetworks/departments&#39;, postings =&gt; {
    if (postings.departments[departmentIndex].jobs.length === 0) { return; }

     $(&quot;#div_block-420-61456&quot;).html(`
      &lt;div id=&quot;job-list&quot;&gt;
      	&lt;h3 class=&quot;dept&quot;&gt;${postings.departments[departmentIndex].name}&lt;/h3&gt;
      	${postings.departments[departmentIndex].jobs.map(item =&gt; `
      		&lt;a href=&quot;${item.absolute_url}&quot;&gt;
      			&lt;h4 class=&quot;job-title&quot;&gt;${item.title}&lt;/h4&gt;
      		&lt;/a&gt;
      		&lt;p class=&quot;job-descrip&quot;&gt;${item.location.name}`
        ).join(&#39;&#39;)}
      &lt;/div&gt;
    `);
  }); 
});

Note: I put the index in a variable so that I could easily test with different departments.

Here is an example fiddle.

huangapple
  • 本文由 发表于 2023年2月14日 06:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定