简体   繁体   中英

Getting value of textbox onchange in javascript

<!DOCTYPE html>
<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
  <style type="text/css">
    #slider { margin: 10px; }
  </style>
 <input type="text" value="" id="rateToPost"/>
<script type="text/javascript">
     var my = document.getElementById("rateToPost").onchange();
     alert(my);
</script>
  <div style="width:75px;height:48px;">
  <div style="width: 5px;margin-left: 10px;">1</div>
  <div style="width: 5px;margin-left: 38px;margin-top: -10px;">2</div>
  <div style="width: 5px;margin-left: 70px;margin-top: -13px;">3</div>
  <div style="width: 5px;margin-left: 98px;margin-top: -12px;">4</div>
  <div style="width: 5px;margin-left: 124px;margin-top: -12px;">5</div>
  </div>

  <script>
  $(document).ready(function() {
    $("#slider").slider({
                range: "min",
                value: 2,
                min: 1,
                max: 5,
    //this gets a live reading of the value and prints it on the page
                slide: function( event, ui ) {
                    $( "#ratingResult" ).text( ui.value );
                },
//this updates the value of your hidden field when user stops dragging
            change: function(event, ui) { 
                $('#rateToPost').attr('value', ui.value);
            }});
  });
  </script>

</head>
<body style="font-size:62.5%;">
<div id="slider"></div>
</body>
</html>

In the above script I used a slider for rating. But what is the problem here is I have to fetch the text box value onchange and highlight the corresponding rating number background. But I cant fetch the value. Anyone can help to solve thisproblem. Thanks in advance

Try this using jQuery:

$('#rateToPost').on("change", function () {
  alert($(this).val());
});

由于$('#rateToPost')是一个表单元素,因此请使用val()方法。

assign value: $('#rateToPost').val(ui.value);
read value: var res = $('#rateToPost').val();
 $('#rateToPost').keyup(function(){
       $('#slider').slider({value: this.value});
    });

Demo http://jsbin.com/azosab/2/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM