Мое раскрывающееся меню работает, когда у меня есть только 1 строка полей ввода, но когда я нажимаю «добавить», чтобы добавить новую строку полей ввода, теперь это не работает.
Я хочу, чтобы новая строка, которую я добавляю, включала «Пожалуйста, укажите» всякий раз, когда я выбираю «другое».
Примечание: этот «Больничный» — это выпадающий список.
<table id = "main_table" class = "table">
<thead>
<tr>
<td class = "border">No.</td>
<td class = "border">Name</td>
<td class = "border">Request Type</td>
<td class = "border">Add. Information / Comment</td>
<td class = "border">action</td>
</tr>
</thead>
<tbody>
<tr>
<td class = "border"><center><span id = "itemNum" style = "font-size: 23px;">0</span></center></td>
<td><input type = "text" name = "" class = "form-control" name = "requestFor" placeholder = "Teacher's name" id = "floatingTextarea1"></td>
<td>
<div class = "row">
<div class = "col">
<select name = "requestType" onclick = "checkRequestType()" id = "requestType" class = "form-control" required>
<option value = "Sick leave">Sick leave</option>
<option >Other</option>
</select>
</div>
<div class = "col">
<input type = "text" name = "requestType" id = "OtherType" value = "" id = "school" class = "form-control" disabled placeholder = "Please Specify">
</div>
</div>
</td>
<td>
<input type = "text" name = "" class = "form-control" name = "description" placeholder = "Leave additional information/comment here" id = "floatingTextarea2">
</td>
<td>
<button type = "button" name = "remove" id = "remove" class = "btn btn-danger"><i class = "bi bi-x-lg"></i></button>
</td>
</tr>
</tbody>
</table>
<button class = "btn btn-success" type = "button" name = "add" id = "add">ADD</button>
Вот javascript, который я использовал для добавления или добавления новой строки в таблицу.
<script type = "text/javascript">
//---------------------------------//
//script to add new input fields---//
//---------------------------------//
$(document).ready(function(){
var html = '<tr><td class = "border"><center><span id = "itemNum" style = "font-size: 23px;">0</span></center></td><td><input type = "text" name = "" class = "form-control" name = "requestFor" placeholder = "Teacher\'s name" id = "floatingTextarea1"></td><td><div class = "row"><div class = "col"><select name = "requestType" onclick = "checkRequestType()" id = "requestType" class = "form-control" required><option value = "Sick leave">Sick leave</option><option value = "Sick leave">Sick leave</option><option value = "Sick leave">Sick leave</option><option value = "Sick leave">Sick leave</option><option >Other</option></select></div><div class = "col"><input type = "text" name = "requestType" id = "OtherType" value = "" id = "school" class = "form-control" disabled placeholder = "Please Specify"></div></div></td><td><input type = "text" name = "" class = "form-control" name = "description" placeholder = "Leave additional information/comment here" id = "floatingTextarea2"></td><td><button type = "button" name = "remove" id = "remove" class = "btn btn-danger"><i class = "bi bi-x-lg"></i></button></td></tr>';
var x = 1;
var num = 1;
$('#add').click(function(){
if (true) {
$("#main_table").append(html);
x++;
}
});
$('#main_table').on('click','#remove',function(){
$(this).closest('tr').remove();
x--;
});
});
</script>
Вот javascript, который я использовал, чтобы проверить, содержит ли раскрывающийся список «другое», чтобы включить поле ввода рядом с ним.
<script type = "text/javascript">
function checkRequestType(){
if (document.getElementById("requestType").value == "Other"){
document.getElementById("OtherType").disabled = false;
console.info("true");
}else{
document.getElementById("OtherType").disabled = true;
console.info("false");
}
}
</script>
<script src = "https://code.jquery.com/jquery-3.6.0.js" ></script>
Ваша функция не будет работать, потому что вы пытались получить элементы по идентификатору, который выберет только первое вхождение элемента.
Поэтому вы можете использовать «этот» и «ближайший» вот так
function checkRequestType(e){
var element = e;
var target = element.parentElement.nextSibling.firstChild;
if (element.value == "Other"){
target.disabled = true;
}else{
target.disabled = false;
}
}
Добавить эту функцию, как это
<select onchange = "checkRequestType(this)">
Это не сработало, сэр, даже после того, как я добавил «это» в параметр функции.
Вы можете использовать этот код, я проверил это
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<title>Document</title>
<script src = "https://code.jquery.com/jquery-3.6.0.js"></script>
</head>
<body>
<table id = "main_table" class = "table">
<thead>
<tr>
<td class = "border">No.</td>
<td class = "border">Name</td>
<td class = "border">Request Type</td>
<td class = "border">Add. Information / Comment</td>
<td class = "border">action</td>
</tr>
</thead>
<tbody>
<tr>
<td class = "border">
<center><span id = "itemNum" style = "font-size: 23px;">0</span></center>
</td>
<td><input type = "text" name = "" class = "form-control" name = "requestFor" placeholder = "Teacher's name"
id = "floatingTextarea1"></td>
<td>
<div class = "row">
<div class = "col">
<select name = "requestType" id = "requestType" class = "form-control request_type" required>
<option value = "Sick leave">Sick leave</option>
<option>Other</option>
</select>
</div>
<div class = "col">
<input type = "text" name = "requestType" id = "OtherType" value = "" id = "school" class = "form-control" disabled = "disabled"
placeholder = "Please Specify">
</div>
</div>
</td>
<td>
<input type = "text" name = "" class = "form-control" name = "description"
placeholder = "Leave additional information/comment here" id = "floatingTextarea2">
</td>
<td>
<button type = "button" name = "remove" id = "remove" class = "btn btn-danger"><i class = "bi bi-x-lg"></i></button>
</td>
</tr>
</tbody>
</table>
<button class = "btn btn-success" type = "button" name = "add" id = "add">ADD</button>
<script type = "text/javascript">
//---------------------------------//
//script to add new input fields---//
//---------------------------------//
$(document).ready(function () {
var html = '<tr><td class = "border"><center><span id = "itemNum" style = "font-size: 23px;">0</span></center></td><td><input type = "text" name = "" class = "form-control" name = "requestFor" placeholder = "Teacher\'s name" id = "floatingTextarea1"></td><td><div class = "row"><div class = "col"><select name = "requestType" id = "requestType" class = "form-control request_type" required><option value = "Sick leave">Sick leave</option><option value = "Sick leave">Sick leave</option><option value = "Sick leave">Sick leave</option><option value = "Sick leave">Sick leave</option><option >Other</option></select></div><div class = "col"><input type = "text" name = "requestType" id = "OtherType" value = "" id = "school" class = "form-control" disabled placeholder = "Please Specify"></div></div></td><td><input type = "text" name = "" class = "form-control" name = "description" placeholder = "Leave additional information/comment here" id = "floatingTextarea2"></td><td><button type = "button" name = "remove" id = "remove" class = "btn btn-danger"><i class = "bi bi-x-lg"></i></button></td></tr>';
var x = 1;
var num = 1;
$('#add').click(function () {
if (true) {
$("#main_table").append(html);
x++;
}
});
$('#main_table').on('click', '#remove', function () {
$(this).closest('tr').remove();
x--;
});
$('body').on('change', '.request_type',function() {
var element = $(this);
var target = element.parent().parent().next().children();
console.info(target)
if (element.val() == "Other") {
console.info(element.parent().parent().find('#OtherType'))
// target.prop("disabled", false);
element.parent().parent().find('#OtherType').removeAttr("disabled")
} else {
target.attr('disabled', true);
}
})
});
</script>
</body>
</html>
Спасибо, сэр, теперь это работает. я только что изменил оператор else, чтобы снова отключить поле «пожалуйста, укажите», если я изменю значение раскрывающегося списка. Еще раз спасибо :D
Почему checkRequestType(e) имеет параметр e? какое значение я должен поставить там?