简体   繁体   English

如何多次调用函数?

[英]How to call function more than once?

I have this code I am trying to implement that calls a function 我正在尝试执行调用函数的这段代码

<body>
    <nav id="menu">
        <div id="app">
            <ul class="List">
                <li><span>Enter Number below</span></li>
                <div id="coachid">
                    <input type="text" name="coachid" id="textbox1" value="22984">
                </div>              
                <li><span>Fitness Programs</span>
                    <ul id="programs">
                        <li class="Label">&nbsp;</li>
                        <li><span>10-Minute Trainer</span>
                            <ul id="10min">
                                <li class="Label">&nbsp;</li>
                                <li><span>Base</span>
                                    <ul id="10minBase">
                                        <li class="Label">&nbsp;</li>
                                        <label for="basic">Text Input:</label>
                                        <input type="hidden" name="basic" id="basic" value="10MinTrainer"/>
                                        <button id="getWebSite">Open Website</button><br/>
                                    </ul>
                                </li>
                                <li><span>Deluxe</span>
                                    <ul id="10min">
                                        <li class="Label">&nbsp;</li>
                                        <input type="hidden" name="basic1" id="basic1" value="TMT"/>
                                        <button id="getWebSite">Open Website</button><br/>
                                    </ul>
                                </li>
                                <li><span>Deluxe</span>
                                    <ul id="Deluxe">
                                        <li class="Label">&nbsp;</li><input type="hidden" name="basic3" id="basic3" value="TMTUpgrade"/>
                                        <button id="getWebSite">Open Website</button><br/>
                                    <ul>
                                </li>                                
                            </ul>
                        </li>
                    </li>
                <ul>
                </li>               
            </ul>
        </div>
    </nav>
</body>

and here is the function 这是功能

$(function() {              
    $('#getWebSite').unbind().click(function(){               
        goUrl = 'http://www.example.com/' + $("#basic").val() + '?referringRepId=' + $("#textbox1").val();
        window.location = goUrl;       
    });             
});

I am trying to call the function multiple times throughout the code to redirect the button to a site based on info held within the html. 我试图在整个代码中多次调用该函数,以根据html中包含的信息将按钮重定向到站点。 Is this possible? 这可能吗?

You need to name your function, apply a class to those elements that should have the click handler, and create the click function: 您需要命名函数,将类应用于应该具有单击处理程序的那些元素,然后创建click函数:

<button class="myClass">

$(function () {
    function doSomething() {
        goUrl = 'http://www.example.com/' 
          + $("#basic").val() 
          + '?referringRepId=' 
          + $("#textbox1").val();

        window.location = goUrl;
    }

    $('.myClass').unbind().click(function () {
        doSomething();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM