1

我有一个触发函数来更改值的html表单,参考:http: //jsfiddle.net/ZvuMh/3/ 如何avalue在另一个函数中动态使用?

HTML:

<select id="sort_post_date">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
    <option value="f">f</option>    
</select>​

变值函数:

    var avalue='';
function getSelectData(id) {


    // set value to be the current selected value
    avalue = jQuery(id+" option:selected").val();

    // change value whenever the box changes
    jQuery(id).change(function () {
        avalue = jQuery(id+" option:selected").val();
        console.log("I see a change! -> "+avalue);
    });

    console.log(avalue);
    return avalue;
}

var d = getSelectData("#sort_post_date");
console.log(d);​

avalue当函数 getSelectData 将其更改为另一个字母时要继承的另一个函数:

function somefunction(){
    var test = "foo"+avalue;
}
4

2 回答 2

2

为什么不直接这样调用呢,这样somefunction每次字母变化时都会调用:

// change value whenever the box changes
jQuery(id).change(function () {
    avalue = jQuery(id+" option:selected").val();
    console.log("I see a change! -> "+avalue);
    somefunction(avalue);
});

function somefunction(avalue){
    var test = "foo"+avalue;
}
于 2012-08-05T07:04:06.320 回答
1

使用该变量的全局范围,就像这样window.avalue = 'value'并使用window.avalue

于 2012-08-05T07:02:11.530 回答