jQuery - dynamic concatenation of strings resulting from two button sets
I'm trying to get jQuery to concatenate two strings in an input field, where each string is the result of the user pressing one button each in two button lists. The field value should update each time a different button is p开发者_JS百科ressed, and thus show the latest combination of buttons used.
Here is how far I got:
http://jsfiddle.net/Argoron/YYNSm/
Thanks for your help
I think http://jsfiddle.net/ThiefMaster/YYNSm/10/ is what you want.
$(document).ready(function() {
var buttonData = [null, null];
$("#lang input:button").click(function() {
buttonData[0] = $(this).val();
if(buttonData[0] && buttonData[1]) {
$("#camouflage").val(buttonData[0] + ', ' + buttonData[1]);
}
});
$("#letters input:button").click(function() {
buttonData[1] = $(this).val();
if(buttonData[0] && buttonData[1]) {
$("#camouflage").val(buttonData[0] + ', ' + buttonData[1]);
}
});
});
Use this jquery, I've done how far i understood your problem
var allowpress=false;
$(document).ready(function() {
$("#lang input[type=button]").click(function() {
$("#camouflage").val($(this).val());
allowpress=true;
});
$("#letters input[type=button]").click(function() {
if(allowpress) {
$("#camouflage").val( $("#camouflage").val() + ',' + $(this).val());
allowpress=false;
}
});
});
you can see it here http://jsfiddle.net/gubhaju/wTFGm/
I'm not sure how you mean, but this code makes the latest combination of language and letter: http://jsfiddle.net/JG6Aw/1/
var lang = '',
letter = '',
$cam = $('#camouflage');
$("#lang input").click(function() {
lang = $(this).val();
update();
});
$("#letters input").click(function() {
letter = $(this).val();
update();
});
var update = function(){
$cam.val(lang + ',' + letter);
};
$(document).ready(function() {
var languages = ['French', 'English', 'Spanish', 'German'];
var letters = ['A', 'B', 'C', 'D', 'E'];
$("input[type=button]").click(function() {
var selections = [];
var previous = $("#camouflage").val().length > 0 ? $("#camouflage").val().split(',') : [];
var isLetter = $.inArray($(this).val(), letters) > -1;
selections = previous;
if (previous.length == 1){
selections[$.inArray(previous[0], languages) > -1 ? 0: 1] = previous[0];
}
selections[isLetter ? 1 : 0] = $(this).val();
$("#camouflage").val(selections.length > 1 ? selections.join(',') : selections[0]);
});
});
精彩评论