Javascript 在javascript中获取文本框onchange的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14338307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:26:29  来源:igfitidea点击:

Getting value of textbox onchange in javascript

javascriptjquery

提问by Rithu

<!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

在上面的脚本中,我使用了一个滑块进行评分。但是这里的问题是我必须在更改时获取文本框值并突出显示相应的评级编号背景。但我无法获取价值。任何人都可以帮助解决这个问题。提前致谢

回答by pala?н

Try this using jQuery:

使用 jQuery 试试这个:

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

回答by Johan

Since $('#rateToPost')is a form element: Use the val()method.

因为$('#rateToPost')是表单元素:使用val()方法。

回答by Anton

 $('#rateToPost').keyup(function(){
       $('#slider').slider({value: this.value});
    });

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

演示http://jsbin.com/azosab/2/edit

回答by Andy

assign value: $('#rateToPost').val(ui.value);
read value: var res = $('#rateToPost').val();