gpt4 book ai didi

JAVASCRIPT:具有自动生成的选定值组合的动态表行

转载 作者:行者123 更新时间:2023-11-28 15:42:33 26 4
gpt4 key购买 nike

我在 SELECT: Combine value of two or more select and get their combination as output 问了我的第一个问题 。现在,在我谷歌搜索之后,我发现了动态表行的创建和删除,并将其用于我的问题。经过一个小时的编码和分析,我得到了这个。我现在的问题如下:
1. 当我只有 1 个选择并且我选择了一个选项时没有任何反应,我将在添加包含另一个选择的新行时工作。
2. 当我删除 1 行时,我输入框中的值没有删除。
3.只能删除最后一行,可以选择吗?

下面的代码如下。我希望你能帮我解决这个问题。

<!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">
<meta name="description" content="">
<meta name="author" content="">
<meta http-equiv="Cache-control" content="no-cache"> <!--The No-Cache-->
<title>Elements Combination</title>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Elements
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<select class="form-control" name='name0' >
<option value="H">Hydrogen</option>
<option value="He">Helium</option>
<option value="Be">Beryllium</option>
<option value="B">Boron</option>
<option value="C">Carbon</option>
<option value="N">Nitrogen</option>
</select>
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
<input type="text" class="form-control" placeholder="Combination Here" name="combination" id="combination" readonly="">
</div>
</div>
<a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
</div>
</body>
</html>

<script type="text/javascript">
$(document).ready(function(){
var i=1;

$("#add_row").click(function(){
$('#addr'+i).html("<td>"+ (i+1) +"</td><td><select class='form-control' name='name"+i+"' ><option value='H'>Hydrogen</option><option value='He'>Helium</option><option value='Be'>Beryllium</option><option value='B'>Boron</option><option value='C'>Carbon</option><option value='N'>Nitrogen</option></select></td>");

$('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
i++;

$('select').on('change', function(){

var s = '';

$('select').each(function(){
if(s == '') {
s = $(this).val();
} else {
s = s + ' + ' + $(this).val() ;
}
})
$('#combination').val(s);
})

});
$("#delete_row").click(function(){
if(i>1){
$("#addr"+(i-1)).html('');
i--;
}
});
});
</script>

最佳答案

考虑使用可重复使用的函数,这将有助于您的代码更加模块化。您可以在每一行上放置一个删除行功能。

请参阅此处示例:https://jsfiddle.net/dcpcq56v/

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<meta http-equiv="Cache-control" content="no-cache"> <!--The No-Cache-->
<title>Elements Combination</title>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Elements
</th>
<th class="text-center" style="width:20%;">
</th>
</tr>
</thead>
<tbody id='element_table'>

</tbody>
</table>
<input type="text" class="form-control" placeholder="Combination Here" name="combination" id="combination" readonly="">
<a id="add_row" class="btn btn-default pull-left">Add Row</a>
</div>
</div>
</div>
</body>
</html>

<script type="text/javascript">
$(document).ready(function(){
var select_item = function(){
$('#combination').val(
$("#element_table select").map(function(index, element){ return $(element).val(); }).get().join("+")
);
};
var add_row = function(){
var new_row = $("<tr><td>"+ ($("#element_table>tr").length+1) +"</td><td><select class='form-control'><option value='H'>Hydrogen</option><option value='He'>Helium</option><option value='Be'>Beryllium</option><option value='B'>Boron</option><option value='C'>Carbon</option><option value='N'>Nitrogen</option></select></td><td><a id='delete_row' class='pull-right btn btn-default'>Delete Row</a></td></tr>").appendTo("#element_table");

select_item();
new_row.find('select').on('change', select_item);
new_row.find("#delete_row").click(function(){
$(this).parents("tr").remove();
select_item();
});
};

add_row();

$("#add_row").click(add_row);

});
</script>

关于JAVASCRIPT:具有自动生成的选定值组合的动态表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43268852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com