Jquery 百里香叶选择器

Jquery selector for thymeleaf

如何从下面 div 中的隐藏字段中获取值:

<div class="col-lg-6" th:each="devicedit : ${eachdevicedetails}">
  <div class="courses-container">
    <div class="course">
      <div
        class="course-preview"
        th:style="${devicedit.color} == 'Green' ? 'background: #03CB61' : 'background: #F22A54'"
      >
        <input type="hidden" name="hiddenDiviceId" th:value="${devicedit.id}" />
        <h6 th:text="${devicedit.area}"></h6>
        <h2 th:text="${devicedit.country}"></h2>
        <br /><br />
        <h6 th:text="${devicedit.state}"></h6>
      </div>
      <div class="course-info">
        <div class="progress-container">
          Status :
          <span
            class="badge badge-pill badge-success"
            th:style="${devicedit.color} == 'Green' ? 'background: #03CB61' : 'background: #F22A54'"
            th:text="${devicedit.color} == 'Green' ? 'Active' : 'Inactive'"
          ></span>
        </div>
        LastUpdate :
        <h6 th:text="${devicedit.lastupdatedon}"></h6>
        <h2 th:text="${devicedit.device_Name}"></h2>
        <br />
        <button
          onclick="openModal(this)"
          id="pickerMaster"
          class="btn btn-primary"
        >
          Detail View
        </button>
      </div>
    </div>
  </div>
</div>

我使用下面的 jquery 代码来获取值,但我只能选择 1st div:

的值
$("#pickerMaster").click(function () {
  var divid = $(this)
    .closest("div.course")
    .find("input[name='hiddenDiviceId']")
    .val();
  console.log(divid);
});

为此,您应该为 button 设置 class 并将其用于获取输入值:

<button onclick="openModal(this)" class="btn btn-primary pickerMaster">Detail View</button>

和:

$(".pickerMaster").click(function() {
var divid = $(this).closest("div.course").find("input[name='hiddenDiviceId']").val();
console.log(divid);
});