我是jQuery的新手,现在我有一个表单根据输入字段中键入的数字提供报价。 现在它只是拉入并除以2.我想从另一个领域拉另一个数字来改变我的等式中可以整除的数字。 所以我们假设我有一个叫做乘数的字段,我想把它放到我有什么格式呢? 0.50就是我想做一个变量,从另一个输入字段拉。
$(function() { // the minimum required value to be entered. // in this case PayPal takes $0.35 from a $1 // donation, hence we ask for at least $1.35 var multiplier = $("#multiplier").val(); var minimum_value = 1.35; // cache elements that are used at least twice var $amount = $("#input_amount"), $brandMatch = $("#autocomplete-ajax"), $msg = $("#msg"), $commission = $("#site_commission_box"); // attach handler to input keydown event $amount.keyup(function(e) { if (e.which == 13) { return; } var amount = parseFloat($amount.val()), //brandMatch = 0.6; commission = amount * multiplier; if (isNaN(commission) || isNaN(amount)) { $msg.hide(); $commission.hide(); return; } if (amount <= minimum_value) { $commission.hide(); $msg .text("Please fill in a higher amount") .fadeIn(); } else { $msg.hide(); $commission .fadeIn() .find("span") //.text((amount - commission).toFixed(2)); .text((amount - commission).toFixed(2)); } });});
I am new to jQuery and right now I have a form that serves up a quote based on the number typed into an input field. Right now it's just pulling in and dividing by 2. I want to pull in another number from another field to change the number divisible in my equation. So let's say I have a field called multiplier and i want to pull that in to what i have how would I format this? The 0.50 is what I'd like to make a variable that pulls from the other input field.
$(function() { // the minimum required value to be entered. // in this case PayPal takes $0.35 from a $1 // donation, hence we ask for at least $1.35 var multiplier = $("#multiplier").val(); var minimum_value = 1.35; // cache elements that are used at least twice var $amount = $("#input_amount"), $brandMatch = $("#autocomplete-ajax"), $msg = $("#msg"), $commission = $("#site_commission_box"); // attach handler to input keydown event $amount.keyup(function(e) { if (e.which == 13) { return; } var amount = parseFloat($amount.val()), //brandMatch = 0.6; commission = amount * multiplier; if (isNaN(commission) || isNaN(amount)) { $msg.hide(); $commission.hide(); return; } if (amount <= minimum_value) { $commission.hide(); $msg .text("Please fill in a higher amount") .fadeIn(); } else { $msg.hide(); $commission .fadeIn() .find("span") //.text((amount - commission).toFixed(2)); .text((amount - commission).toFixed(2)); } });});
最满意答案
为了从使用jQuery的任何地方获取表单的值,使用$("someElementHere").val(); 句法。 实际上,在你调用元素的地方,你表示你想要获取值,但是如果你使用val("Some text here"); 你可以设置元素的新值。 希望这可以帮助!
例如: <input id="multiplier" value="2">然后在JS var multiplier = $("#multiplier").val(); 那么只需在任何地方使用multiplier var!
同样作为一个说明,我使用value="2"因为它可以让你看到/ 2的默认值,但你可以使用任何默认值!
In order to get a value from a form anywhere using jQuery, use the $("someElementHere").val(); syntax. Essentially where you are calling the element you are denoting that you'd like to grab the value, though if you use val("Some text here"); you can then set the element's new value. Hope this helps!
Example: <input id="multiplier" value="2"> Then in JS var multiplier = $("#multiplier").val(); then just use the multiplier var anywhere!
Also as a note, I use value="2" because it will allow you to see a default value of /2, but you could use any default value!
更多推荐
发布评论