Its very basic question. I need your help. There is my variable
var total=$("#bhs").attr("value")*2;
$('div#coupon').append(total);
BUT I want to change the variable on the current page and reflect the result same time how can I do that?
Thanks
it s kind of a bet slip
user input *2 = reflect same time results.
( text input ) (Result)
user inputs 3
jquery multiples 2
and shows the result near the textbox
You can set the value of "value" as such with jQuery:
$("#bhs").attr("value", total);
If I understood your question correctly, you probably want to dispatch a custom event via .trigger() that listens for a change in the variable (via a setter function probably).
var total=$("#bhs").attr("value")*2;
$("#coupon").html(total);
This should take the value of #bhs, multiply it by two, and then place the total in the coupon div.
var total = Number($("#bhs").attr("value")) * 2;
$("div#coupon").html(total);
If you're talking about updating a textbox, you'll need to poll for changes in the box using setInterval
.
This way you'll be able to update the value as the user types, alternatively you could do the update in the blur
event once the user exits the focus on the textbox.
Something like this:
setInterval(updateValueFromTextBox,200);
function updateValueFromTextBox()
{
var newNumber = $("textBox").attr("bla") * 2;
$("resultOnPage").html(newNumber);
}
or
$("textBox").blur(updateValueFromTextBox);
If you use the polling method, remember that polling for the lifetime of the page could be a bit costly. More likely you'll want to start polling on focus
of the textbox and stop polling on blur
.
精彩评论