English 中文(简体)
Increment value of textinput with jquery like spinner
原标题:

Trying to create a simple spinner effect with Jquery, i.e. two buttons (up & down) with a text field. The upbutton increases the value while the down button decreses the value. increment steps + or - 1.

Any suggestions as ui.spinner is most def. not working and I am new to jquery. musty be something like $(#up).click (function ( /*SOMETHING GOES IN HERE but what?*/ )) and likewise for #down. both to set adjust the input text field say id #test as above.

最佳回答

Maybe something like:

$(document).ready( function() {
  var el = $( #test );
  function change( amt ) {
    el.val( parseInt( el.val(), 10 ) + amt );
  }

  $( #up ).click( function() {
    change( 1 );
  } );
  $( #down ).click( function() {
    change( -1 );
  } );
} );
问题回答

Demo: http://jsbin.com/akiki

<button id="inc">+</button>
<button id="dec">-</button>
<input type="text" name="qty" value="0" />

<script type="text/javascript">
  $(function(){
    $("#inc").click(function(){
      $(":text[name= qty ]").val( Number($(":text[name= qty ]").val()) + 1 );
    });
    $("#dec").click(function(){
      $(":text[name= qty ]").val( Number($(":text[name= qty ]").val()) - 1 );
    });
  });
</script>

Have a look at this demo - I have found that this works the best of all the ones I ve tried

Especially if you are using jquery ui themes

http://btburnett.com/spinner/example/example.html

With the base code by @rfunduk I created this:

$(document).ready( function() {

    var el = $( #quantity_wanted );

    function change( amt ) {

        if (el.val() ==   ) {
            var newValue = 1;
        } else {
            var newValue = parseInt( el.val(), 10 ) + amt;
        }

        if (newValue > 0) {
            el.val( newValue );
        }

    }

    $( #cart_quantity_up ).click( function() {
        change( 1 );
    } );

    $( #cart_quantity_down ).click( function() {
        change( -1 );
    } );
} );




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

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.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签