如何将表单中的值存储在变量中以供以后在函数中使用?

时间:2013-12-12 22:05:19

标签: javascript html css

我想制作一个颜色选择器来改变用户创建的颜色的不透明度。 html基本上包含一个带有document.getElementById().value输入的表单。我的问题是:如何将输入中的值存储在变量中,以便我能够更改从用户获得的rgb颜色的不透明度? 这是从表单中获取所有颜色的函数。

function generateColor() { // generate colors and save them for later on
    var red = document.getElementById("red").value;
    var green = document.getElementById("green").value;
    var blue = document.getElementById("blue").value;

    return "rgb(" + red + "," + green + "," + blue + ")";
}

4 个答案:

答案 0 :(得分:0)

你的意思是:

function generateColor() {  // generate colors and save them for later on
 var red = getElementById("red").value,
     green = getElementById("green").value,
     blue = getElementById("blue").value,
     rgb = "rgb(" + red + "," + green + "," + blue + ")";

然后您可以像使用rgb

一样使用red, green and blue

答案 1 :(得分:0)

在任何函数之外声明var storedValue = 0。然后在你的函数中,不带 var,设置它的值。

 function generateColor() {  // generate colors and save them for later on
     var red = getElementById("red").value;
     var green = getElementById("green").value;
     var blue = getElementById("blue").value;
     storedValue = blue;
     return "rgb(" + red + "," + green + "," + blue + ")";

答案 2 :(得分:0)

也许这会有所帮助。所有数据都存储在一个数组中以供引用:

function generateColor() {  // generate colors and save them for later on
     var red = getElementById("red").value;
     var green = getElementById("green").value;
     var blue = getElementById("blue").value;

var colors = new Array();
colors[red] = red;
colors[green] = green;
colors[blue] = blue;

return(colors);
}

答案 3 :(得分:0)

尝试制作构造函数:

// common.js
//<![CDATA[
var doc = document, bod = doc.body;
function E(e){
  return doc.getElementById(e);
}
//]]>

// other.js
//<![CDATA[
function generateColor(redId, greeId, blueId, opacityId, whereId){
  this.red = 0; this.green = 0; this.blue = 0; this.opacity = 1;
  this.getRed = function(red_id){
    if(!red_id)red_id = redId;
    var r = this.red = E(red_id).value;
    return r;
  }
  this.getGreen = function(green_id){
    if(!green_id)green_id = greenId;
    var g = this.green = E(green_id).value;
    return g;
  }
  this.getBlue = function(blue_id){
    if(!blue_id)blue_id = blueId;
    var b = this.blue = E(blue_id).value;
    return b;
  }
  this.getOpacity = function(opacity_id){
    if(!opacity_id)opacity_id = opacityId;
    var o = this.opacity = E(opacity_id).value;
    return o;
  }
  this.build = function(where_id){
    if(!where_id)where_id = whereId;
    var e = this.where = E(where_id), o = this.opacity;
    e.style.color = 'rgb('+this.red+', '+this.green+', '+this.blue+')';
    e.style.opacity = o.toString();
    e.style.filter = 'alpha(opacity='+100*(+o)+')';
  }
}

现在你可以做到:

var wow = new generateColor;
wow.getRed('red'); wow.getGreen('green'); wow.getBlue('blue');
wow.getOpacity('opacity'); wow.build('outputId');
//]]>

var wow = new generateColor('red', 'green', 'blue', 'opacity', 'outputId');
wow.build();
//]]>

或者您可以设置不基于ID的颜色

var wow = new generateColor;
wow.red = 4; wow.blue = 7; wow.green = 'f'; wow.opacity = 0.5;
wow.build('outputId');
//]]>