English 中文(简体)
根据使用j Query的文字箱值作出的改动
原标题:Change div value based on textbox value using jQuery

I created this fiddle that the div value changes based on your dropdown list selection. http://jsfiddle.net/qSkZW/20/

I want to take it one step further and this is to create a textbox field that if you add for example the number 2 it will change the div value to 240 (in real the 120 you find in the script it will be a php var). If it writes 3 then to 360.

Additionally there must be two limitations.

  1. Prevent them from using non-numeric characters.
  2. The maximum number is pre-configured (from a php variable). Like if the max value is set to 10, if the user writes 30 it will return to 10.

Thank you for your help.

最佳回答

Okay, so first, create a textfield (example id="textfield"), then modify your listener s action to

$("#quantity").change(function () {
    var price = parseInt($(this).val(), 10)*120
    $("#textfield").attr( value=" +price+ " );
    # sets the default value of #textfield to the selection from #quantity
    })
.change()

$("#textfield").blur(function () {
# when the element looses focus (they click/tab somewhere else)
        var valid = /^d{2}$/;
        # this means any 2 numbers. change {2} to {3} or however many digits.
        # you can also also use d[0-10] to identify a range of numbers.

    if ( $(this).text() != valid ) {
        $("#message").text("Only numbers please!");
        $("#message").fadeIn();
        # add <span id="message" style="display:none;"></span> next to #textfield
        # enclose #textfield & #message in the same element (like <li> or <div>)
    }
    else {
        $("#message").fadeOut();
    }
});

Could you please provide more info about #2 (I don t have a clear understanding of what is supposed to happen)

Quick question: why are you trying to put a price in an editable element?

问题回答

http://jsfiddle.net/ebiewener/pBeM8/“rel=“nofollow”http://jsfiddle.net/ebiewener/pBeM8/

您将关键降级活动与投入箱捆绑在一起,以防止出现未受遗嘱的特性,并对关键活动进行约束,以便检查价值和营地; 改变iv的高度。

$( input ).keydown(function(e){
   if(!(e.which >= 48 && e.which <=58) && e.which !== 8 && e.which !== 46 && e.which !== 37 && e.which !== 39){ //prevent anything other than numeric characters, backspace, delete, and left & right arrows
       return false;
   }
})
.keyup(function(){
   val = $(this).val();
   if(val > 10){
       val = 10;
       $(this).val(10);
   }
   $( div ).height(val * 10);
});
var max = 30;
$( input ).keyup(function() {
    var v = $(this).val();
    v = isNaN(v)?  :v;
    v = v > max ? max : v;
    $(this).val(v);
    $( div ).html(v * 120);
});

rel=“nofollow> http://jsfiddle.net/vWf2x/2/。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签