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

javascript - How to get dropdown changed value from in jquery to another function - Stack Overflow

programmeradmin1浏览0评论

This is my code

$(function(){
    $('#dropdown_name').change(function(){
        var V = $('#dropdown_name').val();
        var T = $('#dropdown_name :selected').text();
    });
});

I want to send V and T's value below function

function xyz()
{
    a = V;
    b = T;
    alert(a);
    alert(b);
} 

In Function xyz, I have to call many function so I don't want, it take as parameter of xyz function. So how can I get data in another way when I will select a dropdown then it will display alert(a and b) that value and text of dropdown.

How to solve it, please any suggestion.

This is my code

$(function(){
    $('#dropdown_name').change(function(){
        var V = $('#dropdown_name').val();
        var T = $('#dropdown_name :selected').text();
    });
});

I want to send V and T's value below function

function xyz()
{
    a = V;
    b = T;
    alert(a);
    alert(b);
} 

In Function xyz, I have to call many function so I don't want, it take as parameter of xyz function. So how can I get data in another way when I will select a dropdown then it will display alert(a and b) that value and text of dropdown.

How to solve it, please any suggestion.

Share Improve this question edited Mar 14, 2013 at 19:19 dfsq 193k26 gold badges242 silver badges259 bronze badges asked Mar 14, 2013 at 19:15 MD. ABDUL HalimMD. ABDUL Halim 7223 gold badges12 silver badges32 bronze badges 1
  • 1 You don't want to pass the values as arguments to the function because you are calling other functions? How does this make sense? I don't see any reason not to pass them as arguments. It would be the best solution. – Felix Kling Commented Mar 14, 2013 at 19:18
Add a ment  | 

5 Answers 5

Reset to default 2

You can use global variables instead:

function xyz()
{
    a = window.V;
    b = window.T;
    alert(a);
    alert(b);
} 

You will call the function like so:

$(function(){
    $('#dropdown_name').change(function(){
    window.V = $('#dropdown_name').val();
    window.T = $('#dropdown_name :selected').text();
    });
});

if you dont want to pass as parameters you would likely need to define the variables in a scope available to all functions that will use the variables.

var V,T;
$(function(){
    $('#dropdown_name').change(function(){
    V = $('#dropdown_name').val();
    T = $('#dropdown_name :selected').text();
    xyz();
    });
});

function xyz(){
    alert(V); alert(T)
}

you can use global values too

var V = ""; 
var T = "";
$(function(){
    $('#dropdown_name').change(function(){
        V = $('#dropdown_name').val();
        T = $('#dropdown_name :selected').text();
    });
});
I want to send V and T's value below function

function xyz()
{
    a = V;
    b = T;
    alert(a);
    alert(b);
} 

you can call your function instead of the anonymous callback on change like if you have fol markup

<select>
    <option value=""> select </option>
    <option value="2">SO</option>
    <option value="3">google</option>
</select>

js

function callback(e){
    console.log($(e.target).val());

}

$(function(){
 $("select").change(callback);
});

you can replace the callback function with xyz

DEMO

Call xyz from the change event and assign the variables in xyz.

$("#dropdown_name").change(function() {
    xyz();
});

function xyz() {
    var a = $("#dropdown_name option:selected").val();
    alert(a);
}

Demo: http://jsfiddle/D48FY/

发布评论

评论列表(0)

  1. 暂无评论