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

javascript - Passing string into Onchange - Stack Overflow

programmeradmin0浏览0评论

I am having a hard time passing a string constant into an onchange event. code example which i am trying:

var rvalue = "hi";
var r1 = '<label style="vertical-align:2px;">Group by: </label>' +
    '<select id="pwx_documents_range_groupby" onchange="ravi('+rvalue+');">' +
    '<option value="listview" selected="selected">List View</option><option value="notetype">Note Type</option><option value="author">Author View</option>'+
    '<option value="cat">Cat</option></select>'

This is just sample code.. But the question is once drop down change it will call ravi function and throw me the error 'hi' is undefined. If i am passing 0 instead of hi it works great. I am totally surprise why it will not accept the string.

Anybody have any idea that would be great help.

I am having a hard time passing a string constant into an onchange event. code example which i am trying:

var rvalue = "hi";
var r1 = '<label style="vertical-align:2px;">Group by: </label>' +
    '<select id="pwx_documents_range_groupby" onchange="ravi('+rvalue+');">' +
    '<option value="listview" selected="selected">List View</option><option value="notetype">Note Type</option><option value="author">Author View</option>'+
    '<option value="cat">Cat</option></select>'

This is just sample code.. But the question is once drop down change it will call ravi function and throw me the error 'hi' is undefined. If i am passing 0 instead of hi it works great. I am totally surprise why it will not accept the string.

Anybody have any idea that would be great help.

Share Improve this question edited Jun 28, 2012 at 17:08 Joseph Marikle 78.7k18 gold badges113 silver badges130 bronze badges asked Jun 28, 2012 at 17:07 raviravi 711 gold badge3 silver badges11 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

Either set rvalue = "'hi'" or quote it in the body: 'onchange="ravi(\''+rvalue+'\');">'

That is because rvalue is a string. You need to wrap strings with quotes. So it should be something like - onchange="ravi(\''+rvalue+'\');">'

发布评论

评论列表(0)

  1. 暂无评论