The article "jQuery Mobile: Read value of Slider" demonstrate how to read slider value when user click on a button. It's modified to automatically detect slider change event here.
In order to detect slider change, we have to add a additional element("my-slider" in my example)surround the slider - I don't know why I can't use "slider" directly!
Then implement:
$("#my-slider").change(function() {
//...do what you want here...
});
example:
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, height = device-height" />
<title>jQuery Mobile: Slider</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/latest/jquery.mobile.css" />
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>
</head>
<body>
<div data-role="page" id="home">
<div data-role="header">
<h1>Slider</h1>
</div>
<div data-role="content">
<p>jQuery Mobile: Slider</p>
<div data-role="fieldcontain">
<label for="slider">Slider:</label>
<div id="my-slider">
<input type="range" name="slider" id="slider" value="0" min="0" max="100" />
</div>
<input type="button" value="Read Slider" onclick="button_clicked()" />
<p id="slider_value">0</p>
</div>
</div>
<div data-role="footer">
<h4><a href="http://mobile-web-app.blogspot.com/">http://mobile-web-app.blogspot.com/</a></h4>
</div>
</div>
<script>
function button_clicked(){
var SliderValue = $("#slider").val();
alert("Slider value = " + SliderValue);
}
$("#my-slider").change(function() {
var SliderValue = $("#slider").val();
$("#slider_value").text("Slider changed: " + SliderValue);
});
</script>
</body>
</html>
No comments:
Post a Comment