简体   繁体   English

在JavaScript中获取文本框onchange的价值

[英]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. 但是这里的问题是我必须获取文本框的值onchange并突出显示相应的等级背景。 But I cant fetch the value. 但是我无法获取价值。 Anyone can help to solve thisproblem. 任何人都可以帮助解决这个问题。 Thanks in advance 提前致谢

Try this using jQuery: 尝试使用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 演示http://jsbin.com/azosab/2/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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