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

JavaScript pass argument this.value to function - Stack Overflow

programmeradmin3浏览0评论

Here is my javascript code, i want to pass current selected value of the option to my js function, in this code i used static number 6.

<select name='project_name' class='required input_field' id='project_name' onchange="sendRequest('GET','getClientName.jsp?ProjectId=6')">
<option value=''>-- Select --</option>
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>

help me to solve this...

Here is my javascript code, i want to pass current selected value of the option to my js function, in this code i used static number 6.

<select name='project_name' class='required input_field' id='project_name' onchange="sendRequest('GET','getClientName.jsp?ProjectId=6')">
<option value=''>-- Select --</option>
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>

help me to solve this...

Share Improve this question asked Sep 25, 2012 at 5:29 Raj AdroitRaj Adroit 3,8885 gold badges33 silver badges44 bronze badges 1
  • @Buzz i tried like this, onchange="sendRequest('GET','getClientName.jsp?ProjectId=this.value')" – Raj Adroit Commented Sep 25, 2012 at 5:31
Add a ment  | 

3 Answers 3

Reset to default 7

Change the string 'getClientName.jsp?ProjectId=6' to

'getClientName.jsp?ProjectId=' + this.options[this.selectedIndex].value)

or

'getClientName.jsp?ProjectId=' + this.value)

but I think the first one is more browser patible.

var selectBox = document.getElementById('project_name');
selectBox.addEventListener('change', function() { 
  sendRequest('GET', 'getClientName.jsp?ProjectId=' + this.value); 
});

It will help you to get the selected option value . Try this link http://jsfiddle/xyaaa/9/.

<html>
     <head>
         <script>
          function getOption(t){
               var val;
                var options = t.options;
                for(var i=0,len=options.length;i<len;i++){
                     var option = options[i];
                     if(option.selected){
                         val = option.value;
                     }
                }
                return val;
            }
             function  sendRequest(method , url){
                   console.log(url);
             }
    </script>
    </head>
            <body>
                <select onchange="var x=getOption(this);sendRequest('GET','getClientName.jsp?ProjectId='+x)">
                    <option value="1">Project 1</option>
                    <option value="2">Project 2</option>
                    <option value="3">Project 3</option>
                </select>
    </body>
</html>
发布评论

评论列表(0)

  1. 暂无评论