I'm trying to make my first jQuery slider, and I have this code:
$("#str").keyup(function(event) {
var data=$("#str").val();
if (data.length>0)开发者_StackOverflow
{
var intdata=SpecialRound(parseFloat(data));
if (intdata>=0 && intdata<=20)
{
$("#str_slider").slider("option", "value", data);
}
else
{
if (intdata<0)
{
$("#str").val("0");
$("#str_slider").slider("option", "value", "0");
}
if (intdata>20)
{
$("#str").val("20");
$("#str_slider").slider("option", "value", "20");
}
}
}
});
Although, I have a problem. When I write something in the input with the ID #str, nothing happens. Though, the sliders works perfect. You can test att http://vpopulus.tk/damage
EDIT: I basicly want to make so when you edit the input, the slider is changed too.
I think your selector $('#str')
is the problem, because your input has no ID #str but just a name attribute with value "str". The correct selector would be $('input[name="str"]')
EDIT: The actual problem is this SpecialRound function because you're using a not-existing function "round()" there. Use this instead:
//Special round
function SpecialRound(f) {
return Math.round(f*100) / 100;
}
if i were you i'd use the change event instead of keyup
for example:
$( "#str" ).change(function() {
var val = this.val(); // you can do your rounding here then...
$("#str_slider").slider( "value", val );
});
By looking at your testpage, you have at line 54 in SpecialRound
var r=round(f*100)/100;
that should be
var r=Math.round(f*100)/100;
精彩评论