Javascript在另一个函数中使用值

时间:2013-10-09 16:49:22

标签: javascript function

<script>
    function OnInit(s, e) {
        var input = s.GetInputElement();
        var MyValue = TextBox.GetValue(); // Question is here
        ASPxClientUtils.AttachEventToElement(input, "click", function (event) {
            s.SetText("");
        });
    }
        function onLostFocus(s, e) {
            if (s.GetText() == '')
                s.SetSelectedIndex(0);
        }
</script>

我想在onLostFocus函数中使用“MyValue”。

如何在onLostFocus函数中获得“MyValue”?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

您必须更改变量scope,以便它可用于两个函数。

<script>
    var MyValue; // define the variable outside the function
    function OnInit(s, e) {
        var input = s.GetInputElement();
        MyValue = TextBox.GetValue();
        ASPxClientUtils.AttachEventToElement(input, "click", function (event) {
            s.SetText("");
        });
    }
    function onLostFocus(s, e) {
        if (s.GetText() == '')
            s.SetSelectedIndex(0);
    }
</script>