如何使用Java脚本获取<label for =“”>值

时间:2019-02-01 05:30:55

标签: javascript html

我的HTML页面中有一些复选框。而且每个组件都有属性。如果选中此复选框,则需要获取值。

以下是我的HTML代码

<div class="col-6 col-12-small">
    <h4>Document Submited</h4>
	<input type="checkbox" id="idcopy" name="checkbox">
	<label for="idcopy">NIC Copy</label>
	<input type="checkbox" id="birthcertificate" name="checkbox">
	<label for="birthcertificate">Birth Certificate</label>
	<input type="checkbox" id="nomineeNic" name="checkbox">
	<label for="nomineeNic">Nominee NIC copy</label>
	<input type="checkbox" id="agreement" name="checkbox">
	<label for="agreement">Agreement</label>
</div>

我已经尝试使用document.getElementById(idcopy).text()

来获取文本。

2 个答案:

答案 0 :(得分:3)

您调用attr(name)来获取所有元素属性。

$(':checkbox').on('click',function() {
  if(this.checked) {
  console.log($(this).next('label').attr('for'));
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-6 col-12-small">
    <h4>Document Submited</h4>
	<input type="checkbox" id="idcopy" name="checkbox">
	<label for="idcopy">NIC Copy</label>
	<input type="checkbox" id="birthcertificate" name="checkbox">
	<label for="birthcertificate">Birth Certificate</label>
	<input type="checkbox" id="nomineeNic" name="checkbox">
	<label for="nomineeNic">Nominee NIC copy</label>
	<input type="checkbox" id="agreement" name="checkbox">
	<label for="agreement">Agreement</label>
</div>

答案 1 :(得分:0)

// Get all input fields
const allInputs = Array.from(document.querySelectorAll('input'));

// Whenever any input field change
const handleInputChange = e => {
  
  // Create a values array for each checked input
  const values = allInputs.reduce((acc, input) =>
  
    // If field is checked
    input.checked
    
      // Add its label content to values array
      ? [...acc, document.querySelector(`label[for="${input.id}"]`).textContent]
      
      // Or return return previous array
      : acc, []);

  console.log(values);
};

// Bind event on all input fields
allInputs.forEach(input => input.addEventListener('change', handleInputChange));
<div class="col-6 col-12-small">
    <h4>Document Submited</h4>
	<input type="checkbox" id="idcopy" name="checkbox">
	<label for="idcopy">NIC Copy</label>
	<input type="checkbox" id="birthcertificate" name="checkbox">
	<label for="birthcertificate">Birth Certificate</label>
	<input type="checkbox" id="nomineeNic" name="checkbox">
	<label for="nomineeNic">Nominee NIC copy</label>
	<input type="checkbox" id="agreement" name="checkbox">
	<label for="agreement">Agreement</label>
</div>

您可能还具有可重复使用的功能,例如:

const getLabel = (labelFor, rootElement = document) =>
  rootElement.querySelector(`label[for="${labelFor}"]`);

// Or to get text content directly:
const getLabel = (labelFor, rootElement = document) =>
  (rootElement.querySelector(`label[for="${labelFor}"]`) || {}).textContent;

// Boolean || is there to avoid throwing error when no matching label found in DOM

尽管如此,对于更简单的代码,您可以将想要从复选框中获取的数据直接放置在checkbox属性(例如:<input type="checkbox" id="myId" data-myCustomData="My data if box is checked" />)或JS对象中:

const myData = {
  checkboxId1: "Data for first checkbox foo",
  checkboxId5: "Data for first checkbox bar",
};