javascript - 根据导入的值 [javascript, jquery] 在表的下拉列表中设置一个值

标签 javascript jquery html css

我有一个用 csv 文件动态填充的表格。我创建了一个表,它本身在某些列中有下拉列表。

我现在想做的是检查一个值是否存在于下拉列表应该存在的单元格中,如果存在则检查它是否存在于下拉列表中如果是则将其设置为在下拉列表中选择否则选择是在默认值下,单元格周围会出现红色边缘。

这是一个 jsFiddle,其中包含我的代码当前的示例:

https://jsfiddle.net/r236uy5k/

编辑:我更正了我的 jsfiddle: https://jsfiddle.net/kcau7jhd/

$(function(){
	var firstDDM = ['aaa','bbb','ccc','ddd'];
  var firstshortcut = ['a','b','c','d'];
  var option = "",
  		select = "";
  for(var i=0; i<firstDDM.length;i++){
  	option += '<option value="'+ firstshortcut[i] + '">' + firstDDM[i] + '</option>';
  }
  select = '<select class="firstDDM" type="firstDDM">' + option + '</select>';
  
  $("tr").each(function() {
            $(this).find("td:eq(3)").append(select);
        });
});

$(function(){
	var secondDDM = ['Default','AAA','BBB','B1','C'];
  var secondshortcut = ['Default','a','b','b1','c'];
  var option = "",
  		select = "";
  for(var i=0; i<secondDDM.length;i++){
  	option += '<option value="'+ secondshortcut[i] + '">' + secondDDM[i] + '</option>';
  }
  select = '<select class="secondDDM" type="secondDDM">' + option + '</select>';
  
  $("tr").each(function() {
            $(this).find("td:eq(5)").append(select);
        });
});




$("#addRow").click(function(){
        $("#my_id").each(function(){
            var tds='<tr>';
            jQuery.each($('tr:last th', this), function(){
                tds += '<th>' +'<input type="checkbox" name="record" tittle="Delete this row"></input>' + '</th>';
            });
            jQuery.each($('tr:last td', this), function(){
                
                if($('select',this).length){
                    tds+= '<td>' + $(this).html() + '</td>';
                }else{
                    tds+= '<td></td>';
                }
            });
            tds+= '</tr>';
            $('tbody',this).append(tds);
            $('#my_id tbody tr:last').attr("contentEditable", true);
        });
        
    });

 //for the columns that need to be imported with dropdowns create editable option - temporarlly 
    $(function() {
    $("tr").each(function() {
        $(this).find("td:eq(3), td:eq(4),td:eq(5)").attr("contentEditable", true);
        });
    });
    
    //Find and remove selected table rows
    $('#delete-row').click(function(){
        var r = confirm('Are you sure you want to delete them all?');
        $("tbody").find('input[name="record"]').each(function(){
            if($(this).is(":checked")){
                if(r == true){
                    $(this).parents("tr").remove();
                }else{
                    return false;
                }
                
            }
        });
    });
    
    
  
  
table {
            border-collapse: collapse;
            border: 1px black solid;
            font: 12px sans-serif;
            width: 100%;
            table-layout:auto;
            
        }
        td {
            border: 1px black solid;
            text-align: left;
            padding: 2px;
        }

        thead:hover{
            text-decoration: none !important;
        }

        thead tr:first-child{
            color:white;
            text-align: center;
            background-color: #5bc0de;
            padding: 10px;
        }
        tr:nth-child(even){
            background-color: #f2f2f2
        }
        
        tr:hover{
            background-color: #5bc0de;
        }
        button{
            display: inline;
            padding: 50px;
        }
        input button{
            display: inline;
        }
        .dropbtn{
            background-color: blue;
        }
        .table-responsive {
            overflow-y: auto;
            height: 800px;
        }
        .table-responsive table {
            border-collapse: collapse;
            width: 100%;
        }
        .table-responsive thead th{
            position: sticky;
            top: 0;
            background-color: #5bc0de;
            padding: 2px;
        }
        ::-webkit-scrollbar {
            width: 12px;
        }
        ::-webkit-scrollbar-thumb {
            background-color: darkgrey;
            outline: 1px solid slategrey;
        }
        .myButtons{
            display: inline;
            padding: 20px;
        }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
<title>Filtered CSV File</title>
</head>
  <body>
    <h1>
      Filtered CSV FIle
    </h1>
   <br/>
   <br/>
   <div class="myButtons">
     <input type="button" value="Add new row" class="btn btn-info" id="addRow">
     <input  type="button" value="Delete rows" id="delete-row" class="btn btn-info">
   </div>
   <br/>
   <div class="table-responsive">
     <table class="dataframe my_class" id="my_id">
       <thead>
         <tr style="text-align:right;">
           <th> </th>
           <th>col1</th>
           <th>col2</th>
           <th>col3</th>
           <th>col4</th>
           <th>col5</th>
           <th>col6</th>
           <th>col7</th>
           <th>col8</th>
         </tr>
       </thead>
         
       <tbody>
         <tr>
           <th>1</th>
           <td>row1</td>
           <td>row1</td>
           <td>row1</td>
           <td></td>
           <td>row1</td>
           <td>B1</td>
           <td>row1</td>
           <td>row1</td>
         </tr>
         <tr>
           <th>2</th>
           <td>row2</td>
           <td>row2</td>
           <td>row2</td>
           <td></td>
           <td>row2</td>
           <td>AAA</td>
           <td>row2</td>
           <td>row2</td>
         </tr>
         <tr>
           <th>3</th>
           <td>row3</td>
           <td>row3</td>
           <td>row3</td>
           <td></td>
           <td>row3</td>
           <td>C</td>
           <td>row3</td>
           <td>row3</td>
         </tr>
       </tbody>
     </table>
   </div>
 
  </body>
</html>

最佳答案

您可以编辑您输入的选定文本。以下是我认为您想要实现的目标:

  1. 确定下拉列表是否具有特定值,并在可能的情况下预选它们。
  2. 如果下拉列表没有正确的预选值,则将它们标记为错误字段

如果上述几点不正确,请告诉我,我将进行必要的相关更改。

我添加了一个条件检查 if (firstshortcut[i] == $(this).find("td:eq(3)")[0].innerHTML) 你可以替换它具有您想要的任何条件。

替换以下 jquery 片段

$("tr").each(function () {
    var option = "",
        select = "",
        classObject = '',
        isSelected = false;

    if($(this).find("td:eq(3)")[0]){

        for (var i = 0; i < firstDDM.length; i++) {
            if (firstshortcut[i] == $(this).find("td:eq(3)")[0].innerHTML) {
                option += '<option selected="selected" value="' + firstshortcut[i] + '">' + firstDDM[i] + '</option>';
                isSelected = true;
            }
            else
                option += '<option value="' + firstshortcut[i] + '">' + firstDDM[i] + '</option>';
        }
        classObject = !isSelected ? 'errorDropDown' : '';
        select = '<select class="firstDDM' + ' ' + classObject + '" type="firstDDM">' + option + '</select>'

        $(this).find("td:eq(3)").append(select);
    }        
}); 

将这个类添加到css文件中:

.errorDropDown {border: 1px solid red;}

关于javascript - 根据导入的值 [javascript, jquery] 在表的下拉列表中设置一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58605617/

相关文章:

javascript - D3.js 条形图,分割文本

javascript - 根据点击次数增加值(value)

javascript - 在javascript中将两个数组组合成单个多维数组

javascript - jQuery:为匹配选择器的每个元素执行代码

html - 如何最好地在 html 中制作笑脸框

html - 删除 Bootstrap 表单中的空格

javascript - 如何使用 php 将表格数据下载为 excel 表?

javascript - 错误 [ERR_PACKAGE_PATH_NOT_EXPORTED] : No "exports" main resolved in@babel/helper-compilation-targets/package. json

javascript - puppeteer 无法在浏览器中加载 chrome 扩展

html - 图片库定位