Spinner 主要用來輸入各種格式的數(shù)字,可以使用鼠標(biāo)滾輪,鍵盤方向鍵來修改輸入值,也支持直接鍵入數(shù)字。支持本地化的輸入金額和時間。
下面代碼顯示了 Spinner 的基本用法,設(shè)置和取得 Spinner 的當(dāng)前值。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Demos</title>
<link rel="stylesheet" href="themes/trontastic/jquery-ui.css" />
<script src="scripts/jquery-1.9.1.js"></script>
<script src="scripts/jquery-ui-1.10.1.custom.js"></script>
<script>
$(function () {
var spinner = $("#spinner").spinner();
$("#disable").click(function () {
if (spinner.spinner("option", "disabled")) {
spinner.spinner("enable");
} else {
spinner.spinner("disable");
}
});
$("#destroy").click(function () {
if (spinner.data("ui-spinner")) {
spinner.spinner("destroy");
} else {
spinner.spinner();
}
});
$("#getvalue").click(function () {
alert(spinner.spinner("value"));
});
$("#setvalue").click(function () {
spinner.spinner("value", 5);
});
$("button").button();
});
</script>
</head>
<body>
<p>
<label for="spinner">Select a value:</label>
<input id="spinner" name="value" />
</p>
<p>
<button id="disable">Toggle disable/enable</button>
<button id="destroy">Toggle widget</button>
</p>
<p>
<button id="getvalue">Get value</button>
<button id="setvalue">Set value to 5</button>
</p>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/54.png" alt="" />
本例使用兩個 Spinner,以步長為0.001 做為經(jīng)緯度,然后和 Google 地圖配合,通過 Spinner 修改地圖的中心。
此外為了適應(yīng) Google Map API,需要添加對其引用
代碼如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Demos</title>
<link rel="stylesheet" href="themes/trontastic/jquery-ui.css" />
<script src="scripts/jquery-1.9.1.js"></script>
<script src="scripts/jquery-ui-1.10.1.custom.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
$(function () {
function latlong() {
return new window.google.maps.LatLng($("#lat").val(),
$("#lng").val());
}
function position() {
map.setCenter(latlong());
}
$("#lat, #lng").spinner({
step: .001,
change: position,
stop: position
});
var map = new window.google.maps.Map($("#map")[0], {
zoom: 8,
center: latlong(),
mapTypeId: window.google.maps.MapTypeId.ROADMAP
});
});
</script>
<style>
#map {
width:500px;
height:500px;
}
</style>
</head>
<body>
<label for="lat">Latitude</label>
<input id="lat" name="lat" value="44.797" />
<br />
<label for="lng">Longitude</label>
<input id="lng" name="lng" value="-93.278" />
<div id="map"></div>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/55.png" alt="" />