我有一些JQuery UI滑塊具有不同的數據ID。 怎樣才能檢測到誰被選中,並在一個範圍內返回滑塊值?從每個滑塊獲取價值jquery UI
<div id="slider" class="mystyle" data-id="1"></div> <span id="1"></span>
<div id="slider" class="mystyle" data-id="2"></div> <span id="2"></span>
<div id="slider" class="mystyle" data-id="3"></div> <span id="3"></span>
<div id="slider" class="mystyle" data-id="4"></div> <span id="4"></span>
我想這一個,但沒有運氣:
<script>
$(function() {
$("#slider").slider({
range: "max",
min: 0,
max: 5,
create: function() {
$(this).slider("option", "value", $(this).next().val());
},
slide: function (event, ui) {
//get the id of this slider
var id = $(this).attr("data-id")
//select the input box that has the same id as the slider within it and set it's value to the current slider value.
$("span[class*=" + id + "]").text(ui.value);
$("input[class*=" + id + "]").val(ui.value);
}
});
});
</script>
我生成滑塊dynamicaly:
<script>
$(document).ready(function() {
$("#adding").click(function() {
var intId = $("#buildyourform div").length + 1;
var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
var fName = $("<input type=\"text\" class=\"fieldname\" value=\"Enter your Facebook fan page url\" />");
var fType = $("<div id='slider' data-id='"+intId+"' style='width:250px; float:left; margin-left:10px;'></div>").slider();
var fLikes= $("<div class=\"fieldname\" id='"+ intId +"' style='width: 60px; height: 24px; float: left; margin-left: 13px; margin-top: 7px; border: 1px solid #999; color: rgb(243, 20, 145); text-align: center; font-family: serif;' />");
var fCost = $("<div class=\"fieldname\" id=\"fcost" + intId + "\" style='width: 60px; height: 24px; float: left; margin-left: 6px; margin-top: 7px; border: 1px solid #999; color: rgb(243, 20, 145); text-align: center; font-family: serif;' />");
var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");
removeButton.click(function() {
$(this).parent().remove();
});
fieldWrapper.append(fName);
fieldWrapper.append(fType);
fieldWrapper.append(removeButton);
fieldWrapper.append(fLikes);
fieldWrapper.append(fCost);
$("#buildyourform").append(fieldWrapper);
});
});
</script>
的ID應該是唯一的,可能引起的問題。 – badAdviceGuy