Итак, сначала добавьте кнопку в пользовательский интерфейс, например:
<div class="form-group col-sm-12">
<button id="btn" class="btn btn-sm btn-primary">Get Checked Values</button>
</div>
, а затем добавьте addEventListener
для этой кнопки, например:
document.querySelector("#btn").addEventListener("click", function(e){
// Our code logic here...
});
Внутри этого нам просто нужно чтобы проверить, проверено ли check1
или check2
, и на основе этого получить соответствующие значения элементов, например:
if (check2.checked) {
// If second checkbox is selcted - get values of `address`
text = document.querySelector("#address").value
} else if (check1.checked){
// If first checkbox is selected - get values of `name` and `phone`
text = document.querySelector("#name").value + ', '
text += document.querySelector("#phone").value
}
Рабочая демонстрация:
var form1 = document.querySelector("#f1"),
form2 = document.querySelector("#f2"),
check1 = document.querySelector("#checkbox1"),
check2 = document.querySelector("#checkbox2");
check1.onchange = function() {
if (this.checked) {
document.querySelector("#address").closest('.form-group').style.display = "none";
} else {
document.querySelector("#address").closest('.form-group').style.display = "";
}
}
check2.onchange = function() {
if (this.checked) {
document.querySelector("#name").closest('.form-group').style.display = "none";
document.querySelector("#phone").closest('.form-group').style.display = "none";
} else {
document.querySelector("#name").closest('.form-group').style.display = "";
document.querySelector("#phone").closest('.form-group').style.display = "";
}
}
document.querySelector("#btn").addEventListener("click", function(e){
e.preventDefault()
console.clear()
var text = '';
if (check2.checked) {
text = document.querySelector("#address").value
} else if (check1.checked){
text = document.querySelector("#name").value + ', '
text += document.querySelector("#phone").value
}
console.log(text)
});
.hidden { display: none;}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="checkbox1" value="option1">
<label class="form-check-label" for="checkbox1">First</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="checkbox2" value="option2">
<label class="form-check-label" for="checkbox2">Second</label>
</div>
<form id="f1">
<div class="form-row">
<div class="form-group col-sm-6">
<label for="first">Name</label>
<input type="text" class="form-control form-control-sm" id="name" name="name" value="john" required>
</div>
<div class="form-group col-sm-6">
<label for="">Phone</label>
<input type="text" class="form-control form-control-sm" id="phone" name="phone" value="12345" required>
</div>
<div class="form-group col-sm-6">
<label for="inputCity">Address</label>
<input type="text" class="form-control form-control-sm" id="address" name="address" value="berlin" required>
</div>
<div class="form-group col-sm-12">
<button id="btn" class="btn btn-sm btn-primary">Get Checked Values</button>
</div>
</div>
</form>