简体   繁体   中英

How to store the value from a form in a variable for later use in a function?

I want to make a color picker that changes the opacity of the color that was created by the user. The html basically holds a form with inputs which I get with document.getElementById().value . My question is: How can I store the value from the input in a variable so that I would be able to change the opacity of the rgb color that I get from the user? This is the function that gets all the colors from the form.

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 + ")";
}

Do you mean:

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 + ")";

then you can just use rgb in the same way you have used red, green and blue

Declare a var storedValue = 0 outside of any function. Then inside your function, without var, set it's value.

 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 + ")";

Maybe this will help. All data gets stored in an array for referencing:

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);
}

Try making a constructor:

// 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)+')';
  }
}

Now you can do either:

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

or

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

or you can set the colors not based on ids

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM