首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为函数赋值并在单独的按钮jquery上运行

问为函数赋值并在单独的按钮jquery上运行
EN

Stack Overflow用户
提问于 2015-11-26 16:33:47
回答 3查看 40关注 0票数 1

​

代码语言:javascript
复制
$(document).ready(function () {
    function somefunction(input) {
        $('div').html(input);
    }

    $("#assign").click(function () {
        //assign the value only
        somefunction("from assign to putput")
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

​

如何在一个按钮中为函数赋值,并在另一个按钮上运行该函数?

任何建议都会被接受。

EN

回答 3

Stack Overflow用户

发布于 2015-11-26 16:36:47

只需将值赋给函数作用域之外的变量即可。

代码语言:javascript
复制
$(document).ready(function () {
    var assignedvalue = "";
    function somefunction() {
        $('div').html(assignedvalue);
    }

    $("#assign").click(function () {
        //assign the value only
        assignedvalue = "from assign to putput";
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
票数 3
EN

Stack Overflow用户

发布于 2015-11-26 16:36:38

为什么不使用全局变量而不是在函数中赋值?

​

代码语言:javascript
复制
$(document).ready(function () {
   var input = "";
    function somefunction() {
        $('div').html(input);
    }

    $("#assign").click(function () {
        //assign the value only
       input = "from assign to putput";
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

​

如果你不想使用全局变量,你可以先给div属性赋值,如下所示:

​

代码语言:javascript
复制
$(document).ready(function () {
    function somefunction() {
        $('div').html($('div').attr('temp-value'));
    }

    $("#assign").click(function () {
        //assign the value only
       $('div').attr("temp-value","from assign to putput");
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

​

票数 2
EN

Stack Overflow用户

发布于 2015-11-26 16:44:22

​

代码语言:javascript
复制
$(document).ready(function () {
var input="";//Global Variable initially assigned as empty
    function somefunction(input) {
        $('div').html(input);
    }

    $("#assign").click(function () {
        //assign the value only
        input="Some Value";//Assigning the global variable
    });

    $("#output").click(function () {
        //run the function
        somefunction(input);
        input="";//Clear the global variable after the success of 'somefunction'
    });
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33933817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档