如何不止一次调用函数?

时间:2014-07-16 18:36:40

标签: javascript jquery

我有这个代码我试图实现调用函数

<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>

这是函数

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

我试图在整个代码中多次调用该函数,以根据html中保存的信息将按钮重定向到某个站点。这可能吗?

1 个答案:

答案 0 :(得分:2)

您需要命名您的函数,将类应用于应该具有单击处理程序的元素,并创建单击函数:

<button class="myClass">

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

        window.location = goUrl;
    }

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