This has got me for some reason the code below is translating the currency variable to a string and then not getting the actual value from the self named variable...
$("#quantity").change(functio开发者_如何学Pythonn() {
var euroPrice = new Array("12.95", "14.95", "18.95", "21.95");
var gbpPrice = new Array("10.95", "11.95", "16.95", "18.95");
var currency = $("#currency option:selected").val();
var currently = $("#quantity option:selected").val();
$("#Price").text( currency[currently]);
$("#amount").attr("value", currency[currently]);
});
Where #currency and #quantity are two select boxes in the HTML that I need the code to evaluate their currently selected values on changes, retrieving the correct value from the correct variable.
Any help greatly received :)
m
I can't see the HTML...but the only thing that makes sense to me as to why you aren't using the two defined arrays is if currency is a dropdown with 2 potential values 'euroPrice' and 'gbpPrice' and you are trying to use this as a dynamic variable with quantity being the index of the array for either currency. If I'm correct in my guess, then I think what you are missing is an eval() statement. Also, note, that for selects you should not need to use the option:selected in jquery. The val() statement figures this out. I'm also assuming #amount is an input and doesn't need the attr()...so:
var currency = $("#currency").val();
var quantity = $("#quantity").val();
eval("var price = "+currency+"["+quantity+"];");
$("#Price").text( price );
$("#amount").val( price );
Hope that's what you were actually aiming at. If not, I think seeing the two HTML selects with at least the first few selectable values in each would be greatly beneficial in figuring out what you're trying to do.
I am not sure what you are trying to achieve here exactly, but the complete lack of reference to euroPrice and gbpPrice looks very much like you are simply querying the wrong array for the value.
var currency = $("#currency option:selected").val();
var currently = $("#quantity option:selected").val();
$("#Price").text( currency[currently]);
$("#amount").attr("value", currency[currently]);
According to this code, currency will be a single value, namely the value attribute of the selected item in your currency selector, while currently will be also a single value, namely the value of the selected item of your quantity select box.
In the second pair of rows you are trying to reference currency[currently] which is trying to access a single string as an array. I think this is where you made the mistake. Isn't it possible that you wanted to have something like
$("#Price").text( euroPrice[currently]);
$("#amount").attr("value", euroPrice[currently]);
instead (in case you want the euro equivalent of the currency)?
精彩评论