最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting value from a range input (slider) - Stack Overflow

programmeradmin6浏览0评论

I am adding a slider dynamically on a page using a string like the one below:

"<input type=\"range\" name=\"aName\" min=\"1\" max=\"9\"/>";

Then, after I append it to the page using plain javascript I can delegate using the following code:

$('input[name=aName]').on('change', function () { alert(this.value)});

and indeed when the value changes, I receive the alert with the correct value. However, if I delegate like this:

$('input[name=aName]').on('change', handleChange);

and define the function later on like this:

function handleChange () {
    var theValue = $('input[name=aName]').value;
    alert("value: " + theValue);
}

I receive the alert with the message "value: undefined". Why is this thing happening?

I am adding a slider dynamically on a page using a string like the one below:

"<input type=\"range\" name=\"aName\" min=\"1\" max=\"9\"/>";

Then, after I append it to the page using plain javascript I can delegate using the following code:

$('input[name=aName]').on('change', function () { alert(this.value)});

and indeed when the value changes, I receive the alert with the correct value. However, if I delegate like this:

$('input[name=aName]').on('change', handleChange);

and define the function later on like this:

function handleChange () {
    var theValue = $('input[name=aName]').value;
    alert("value: " + theValue);
}

I receive the alert with the message "value: undefined". Why is this thing happening?

Share Improve this question asked Oct 31, 2013 at 20:52 MightyMouseMightyMouse 13.9k8 gold badges35 silver badges45 bronze badges 2
  • what is handleChange? there is only handleRQTSliderChange – Venkata Krishna Commented Oct 31, 2013 at 20:53
  • That was a mistake on my side. Clearly it is handleChange in both cases. I have edited the post. – MightyMouse Commented Oct 31, 2013 at 20:54
Add a ment  | 

1 Answer 1

Reset to default 4

This is wrong $('input[name=aName]').value. I think you're mixing javascript & jquery.

For getting the value, use like this.

$('input[name=aName]').val()
发布评论

评论列表(0)

  1. 暂无评论