简体   繁体   中英

jQuery common factor of functions with events

I've got this code:

$(document).ready( function(){
    var gold;
    var silver;
    var copper;

    $("#gold").change(function(){
        gold = $(this).val();
    });
    $("#silver").change(function(){
        silver = $(this).val();
    });
    $("#copper").change(function(){
        copper = $(this).val();
    });
});

It just updates the variable whenever the textfield changes, this is the html:

<input type="text" id="gold">
<input type="text" id="silver">
<input type="text" id="copper">

If i had to declare something like:

$("#copper").change(function(){
    copper = $(this).val();
}); 

for every variable ive got, what can i do when i have over 100 variables? I want to avoid getting elements 1 by 1 with theyr events...

I have tried something like this:

var gold = dynamicValue("gold");

function dynamicValue(element){
    $("#" + element).change(function(){
        return $(this).val();
    });
}

But doesn't seem to work...

If you're structure is always like that I would recommend something like this.

var values = {};

$('input').change(function(){
   values[this.id] = this.value;
});

Like this it will create an object with the IDs as keys and in input values as the key value.

values = {
   gold: 'something',
   copper: 'something',
   silver: 'comething'
}

and you will be able to get them anytime

var gold = values.gold;

If you get undefined it will mean that the input has not yet been changed. Example below

 var values = {}; $('input').change(function() { values[this.id] = this.value; $('div').text(JSON.stringify(values)); // this line is only for the example }); 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="gold"> <input type="text" id="silver"> <input type="text" id="copper"> <input type="text" id="gold1"> <input type="text" id="silver2"> <input type="text" id="copper4"> <div></div> 

HTML

<input type="text" class="metal-input" id="gold">
<input type="text" class="metal-input" id="silver">
<input type="text" class="metal-input" id="copper">

jQuery

var metals = {};

$('.metal-input').on('input', function() {

    metals[this.id] = $(this).val();
    console.log(metals);
});

Use a class and use the id as a key

 $(document).ready( function(){ var metals = { gold : null, silver : null, copper : null} $(".inputs").change(function(){ metals[this.id] = $(this).val(); console.log(metals); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <label for="gold">gold</label><input type="text" id="gold" class="inputs"> <label for="silver">silver</label><input type="text" id="silver" class="inputs"> <label for="copper">copper</label><input type="text" id="copper" class="inputs"> 

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