开发者

Change jquery variable on the fly [closed]

开发者 https://www.devze.com 2023-03-15 07:16 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago. 开发者_如何学C

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消