Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用表列选择两个选项_Javascript_Jquery_Html_Css_Multiple Columns - Fatal编程技术网

Javascript 使用表列选择两个选项

Javascript 使用表列选择两个选项,javascript,jquery,html,css,multiple-columns,Javascript,Jquery,Html,Css,Multiple Columns,我有一个多栏的表格。我有两个选择框: 一个是更改高亮显示列的背景色 一个用于选择要高亮显示的列 这假设我首先单击#colorselect选择框。是否需要先使用内置的#columnselect选择框,然后使用#colorselect函数创建另一个函数 以下是我目前掌握的情况: $(函数(){ $(“#颜色选择”).change(函数(){ $(“#颜色选择选项:已选择”)。每个(函数(){ 如果($(此).attr(“值”)=“红色”){ clr=“红色” } 如果($(this.attr(“

我有一个多栏的表格。我有两个选择框:

  • 一个是更改高亮显示列的背景色
  • 一个用于选择要高亮显示的列
这假设我首先单击
#colorselect
选择框。是否需要先使用内置的
#columnselect
选择框,然后使用
#colorselect
函数创建另一个函数

以下是我目前掌握的情况:

$(函数(){
$(“#颜色选择”).change(函数(){
$(“#颜色选择选项:已选择”)。每个(函数(){
如果($(此).attr(“值”)=“红色”){
clr=“红色”
}
如果($(this.attr(“值”)=“绿色”){
clr=“绿色”
}
if($(this.attr(“value”)=“”){
clr=“黄色”
}
};
$(“#columnselect”).change(函数(){
$(“#columnselect option:selected”)。每个(函数(){
if($(this.attr(“value”)=“column1”){
columnhighlightsel=“Column1”
}
if($(this.attr(“value”)=“column2”){
columnhighlightsel=“Column2”
}
if($(this.attr(“value”)=“”){
columnhighlightsel=“Column2”
}
}

对于(var i=0;i我只需编写一个函数来处理任意一列中的更改。在执行代码之前,检查其中一列是否为空。那么顺序就无关紧要了

然后,只需使颜色中的值选择所需的颜色,并将列的编号设置为列:

$('#colorselect, #columnselect').change(function() {
  var $color = $('#colorselect').val(),
      $column = $('#columnselect').val();
  if ($color && $column) {
    $("#table tbody tr td:nth-child("+$column+")").each(function(){
      $(this).css('background-color', $color);
    };
  }
});
根据您的值,您可能必须调整if条件以使其更具体

 $("#colorselect").change(function(){
           clr = $("#colorselect option:selected").attr("value");
        clr = clr == '' ? 'yellow' : clr;
        color();
    }

   $("#columnselect").change(function(){

         columnhighlightsel= $(this);
        color();
    }

function color(){
   if(!columnhighlightsel || !clr){
      return;
   }
   columnhighlightsel.css('background-color', clr);
}

现在,您必须将columnhighlightsel和clr-变量放入颜色函数上下文。

您可以发布HTML吗?大概,在更改列颜色之前,您需要先选择列,除非您有一个“默认”列……没有问题,但您应该像这样关闭jquery更改函数});而不是像这样};检查更新的问题,您严重遗漏了关闭
})问题。哎呀!谢谢silentw。欢迎来到StackOverflow!请看,其中的共识是“不,他们不应该”。谢谢!这非常有帮助。我知道我让事情变得更复杂了。