颜色下拉及其颜色在动态下拉列表中

时间:2017-12-29 09:50:55

标签: javascript html css

我正在使用引导程序表单来添加打印机。根据流程,将选择材料,然后选择相应的颜色。这些过程在动态下拉列表中运行良好。但我希望与颜色一起显示,以便用户能够正确地了解他所选择的颜色。 请指导我如何使用脚本中的相应颜色传递颜色。用户应该能够看到他正在添加的颜色。

dynamic dropdown

  <script language="javascript">
function fillCategory(){ 
 // this function is used to fill the category list on load
addOption(document.drop_list.Category, "FDM", "FDM", "");
addOption(document.drop_list.Category, "SLA", "SLA", "");
addOption(document.drop_list.Category, "SLS", "SLS", "");
}

function SelectSubCat(){
// ON selection of category this function will work

removeAllOptions(document.drop_list.SubCat);
addOption(document.drop_list.SubCat, "", "Material", "");

if(document.drop_list.Category.value == 'FDM'){
addOption(document.drop_list.SubCat,"ABS", "ABS");
addOption(document.drop_list.SubCat,"PLA", "PLA");
addOption(document.drop_list.SubCat,"Nylon", "Nylon");
addOption(document.drop_list.SubCat,"Hips", "Hips");
addOption(document.drop_list.SubCat,"PC", "PC");
addOption(document.drop_list.SubCat,"Flex", "Flex");
addOption(document.drop_list.SubCat,"Wood Fill PLA", "Wood Fill PLA");
addOption(document.drop_list.SubCat,"Carbon Fiber", "Carbon Fiber");
addOption(document.drop_list.SubCat,"ABS+", "ABS+");
}
if(document.drop_list.Category.value == 'SLA'){
addOption(document.drop_list.SubCat,"Clear Resin", "Clear Resin");
addOption(document.drop_list.SubCat,"ABS Like", "ABS Like");
addOption(document.drop_list.SubCat,"Flex", "Flex");
addOption(document.drop_list.SubCat,"Castable Resin", "Castable Resin");
}
if(document.drop_list.Category.value == 'SLS'){
addOption(document.drop_list.SubCat,"Nylon 12", "Nylon 12");

}

}
function SelectSubCat1(){
// ON selection of category this function will work

removeAllOptions(document.drop_list.SubCat1);
addOption(document.drop_list.SubCat1, "", "Color", "");

if(document.drop_list.Category.value == 'FDM' && document.drop_list.SubCat.value != 'Wood Fill PLA'){
addOption(document.drop_list.SubCat1,"White", "White");
addOption(document.drop_list.SubCat1,"Black", "Black");
addOption(document.drop_list.SubCat1,"Blue", "Blue");
addOption(document.drop_list.SubCat1,"Red", "Red");
addOption(document.drop_list.SubCat1,"Green", "Green");
addOption(document.drop_list.SubCat1,"Grey", "Grey");
addOption(document.drop_list.SubCat1,"Orange", "Orange");
addOption(document.drop_list.SubCat1,"Yellow", "Yellow");
addOption(document.drop_list.SubCat1,"Silver", "Silver");
addOption(document.drop_list.SubCat1,"Glow-in-the-dark", "Glow-in-the-dark");
addOption(document.drop_list.SubCat1,"Purple", "Purple");
addOption(document.drop_list.SubCat1,"Gold", "Gold");
addOption(document.drop_list.SubCat1,"Brown", "Brown");
addOption(document.drop_list.SubCat1,"Pink", "Pink");
}
if(document.drop_list.SubCat.value == 'Wood Fill PLA'){
addOption(document.drop_list.SubCat1,"Brown", "Brown");
}

if(document.drop_list.SubCat.value == 'ABS Like'){
addOption(document.drop_list.SubCat1,"White", "White");
addOption(document.drop_list.SubCat1,"Black", "Black");
addOption(document.drop_list.SubCat1,"Grey", "Grey");
}

if(document.drop_list.SubCat.value == 'Nylon 12'){
addOption(document.drop_list.SubCat1,"White", "White");
}
if(document.drop_list.SubCat.value == 'Clear Resin'){
addOption(document.drop_list.SubCat1,"Transparent/clear", "Transparent/clear");
}
if(document.drop_list.SubCat.value == 'Flex'){
addOption(document.drop_list.SubCat1,"Transparent/clear", "Transparent/clear");
}
if(document.drop_list.SubCat.value == 'Castable Resin'){
addOption(document.drop_list.SubCat1,"Transparent/clear", "Transparent/clear");
}

}
////////////////// 

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	
	selectbox.options.add(optn);
}

</script>
<?php
session_start();
if(empty($_SESSION)){
header("Location: ../login.php");
exit(); }
$mpage = "printer";
$page = "add_printer.php";
include '../header.php';
?>

<!DOCTYPE html>
<html>
 


  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        ADD PRINTER
       
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">Printer</a></li>
        <li class="active">Add Printer</li>
      </ol>
    </section>

	
	<body  onload="fillCategory();">
    <!-- Main content -->
    <section class="content">
	
	<div class="col-md-12">
	 <div class="box box-info">
            <div class="box-header with-border">
              <h3 class="box-title">Printer Details</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form class="form-horizontal" method="post" name="drop_list">
              <div class="box-body">
                <div class="form-group">
                  <label for="inputname" class="col-sm-4 control-label">Printer Name</label>

                  <div class="col-sm-6">
                    <input type="text" class="form-control" id="inputname" placeholder="Printer Name" name="printer_name">
                  </div>
                </div>
				
				<div class="form-group">
				<label for="Process" class="col-sm-4 control-label">Process</label>
				
				<div class="col-sm-6">
				<SELECT  NAME="Category" class="form-control" onChange="SelectSubCat();" > 
				<Option value="">Process</option>
				</SELECT>
				</div>
				</div>

				<div class="form-group">
				<label for="Material" class="col-sm-4 control-label">Material</label>
				<div class="col-sm-6">
				<SELECT id="SubCat" NAME="SubCat[]" class="form-control" onChange="SelectSubCat1();" multiple>
				<Option value="">Material</option>
				
				</SELECT>
		
				</div>
				
				</div>
				
				<div class="form-group">
				<label for="Color" class="col-sm-4 control-label">Color</label>
				<div class="col-sm-6">
						<SELECT id="SubCat1" NAME="SubCat1" class="form-control" >
				<option value="" disabled selected >Color</option>
				</SELECT>
							
				</div>
				
				</div>
				
				
				<div class="form-group">
                  <label for="quality" class="col-sm-4 control-label">Strength</label>
				  <div class="col-sm-6">
				  <select class="form-control" name="strength">
                  <option selected="selected">High</option>
                  <option >Mid</option>
                  <option>Low</option>
                  </select>
				</div>
				</div>
				
				
				
				  <div class="form-group">
                  <label for="surfacefinish" class="col-sm-4 control-label">Surface Finish</label>
				  <div class="col-sm-6">
				  <select class="form-control" name="surface_finish">
                   <option selected="selected">High</option>
                  <option>Mid</option>
                  <option>Low</option>
                  </select>
				</div>
				</div>
				
				<div class="form-group">
				<label for="per_gram_charge" class="col-sm-4 control-label">Per Gram Price&nbsp(Rs.) </label>
				<div class="col-sm-6">
				<input type="number"  class="form-control" id="per_gram_charge" step="0.01" name="per_gram_charge" placeholder="0.00">
				</div>
				</div>
				
				<div class="form-group">
				<label for="per_hour_charge" class="col-sm-4 control-label">Per Hour Price&nbsp(Rs.) </label>
				<div class="col-sm-6">
				<input type="number" class="form-control" id="per_hour_charge" step="0.01" name="per_hour_charge" placeholder="0.00">
				</div>
				</div>
						
				
              </div></div>
              <!-- /.box-body -->
              <div class="box-footer">
                
                <button type ="submit" name="submit" value="submit" class="btn btn-info pull-right" >Add Printer</button>
              </div>
              <!-- /.box-footer -->
            </form></div>
			

		  </div>
		  
    </section>
	</body>

<?php

include '../footer.php';

?>


</html>

2 个答案:

答案 0 :(得分:2)

您可以修改addOption函数以为每个选项添加背景颜色。 (或给他们上课)

function addOption(selectbox, value, text) {
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  if(optn.value != ('Transparent/clear' || 'Glow-in-the-dark')){
    optn.style.background = value
  }
  selectbox.options.add(optn)
}

或者你可以在css

中完成
option[value="Black"] {
  background: black;
}

option[value="Red"] {
  background: red;
}

/* and so on... */

<强>演示

&#13;
&#13;
function addOption(selectbox, value, text) {
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  if(optn.value != ('Transparent/clear' || 'Glow-in-the-dark')){
    optn.style.background = value
  }
  selectbox.options.add(optn);
}

// Just for the demo
const selectBox = document.querySelector('select')
const colors = ['Red', 'Transparent/clear', 'Glow-in-the-dark', 'Blue', 'Goldenrod']

colors.forEach(color => addOption(selectBox, color, color))
&#13;
<select></select>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你可以这样做。

<select>
    <option value="1" style="background:red">Red</option>
    <option value="2" style="background:yellow">Yellow</option>
    <option value="3" style="background:purple">Purple</option>
</select>

修改addOption()方法,如下所示。 调用以下功能时提供颜色名称

function addOption(selectbox, value, text, color) // added extra parameter color.
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    optn.style.backgroundColor = color; // added new statement
    selectbox.options.add(optn);
}

调用函数

  // passing 4th parameter as color blue.
    addOption(document.drop_list.SubCat1,"Blue", "Blue", "blue");