Javascript 加载页面时调用Jquery on click函数

Javascript 加载页面时调用Jquery on click函数,javascript,jquery,Javascript,Jquery,我正在尝试向按钮添加一个函数,但是当页面被加载时会调用该函数。单击按钮时,getMemberByName位于另一个js文件中,并且工作正常 <input type="button" value="miembro" id="boton2" /> <script> $("#boton2").click(getMemberByName('nombre1','apellido1')); </script> $(“#boton2”)。单击(getMembe

我正在尝试向按钮添加一个函数,但是当页面被加载时会调用该函数。单击按钮时,getMemberByName位于另一个js文件中,并且工作正常

<input type="button" value="miembro" id="boton2" />

<script>
    $("#boton2").click(getMemberByName('nombre1','apellido1'));
</script>

$(“#boton2”)。单击(getMemberByName('nombre1','apellido1');

以下是使用jQuery绑定此类事件的正确方法:

<script>
$(function(){
    $('#boton2').click(function(){
        getMEmberByName('nombre1', 'apellido1');
    });
});
</script>

$(函数(){
$('#boton2')。单击(函数(){
getMEmberByName('nombre1','apellido1');
});
});

希望对您有所帮助。

以下是使用jQuery绑定此类事件的正确方法:

<script>
$(function(){
    $('#boton2').click(function(){
        getMEmberByName('nombre1', 'apellido1');
    });
});
</script>

$(函数(){
$('#boton2')。单击(函数(){
getMEmberByName('nombre1','apellido1');
});
});
希望这能有所帮助。

试试以下方法:

$("#boton2").click(function() {
    getMemberByName('nombre1','apellido1');
});
请尝试以下方法:

$("#boton2").click(function() {
    getMemberByName('nombre1','apellido1');
});

您需要将它们包装在函数中:

<script>
    $(document).ready(function(){
        $("#boton2").click(function(){
            getMemberByName('nombre1','apellido1');
        });
    });
</script>

$(文档).ready(函数(){
$(“#boton2”)。单击(函数(){
getMemberByName('nombre1','apellido1');
});
});

文档:

您需要将它们包装在函数中:

<script>
    $(document).ready(function(){
        $("#boton2").click(function(){
            getMemberByName('nombre1','apellido1');
        });
    });
</script>

$(文档).ready(函数(){
$(“#boton2”)。单击(函数(){
getMemberByName('nombre1','apellido1');
});
});

文档:

如@Moosman所说,如果这可以帮助某人,那么您需要将您想要做的事情包装到函数中,并且不能调用。例如:

<script>
    $("#boton2").on("click", function(){getMemberByName('nombre1','apellido1')});
</script>

$(“#boton2”)。在(“单击”,函数(){getMemberByName('nombre1','apellido1')});
或:


$(“#boton2”)。在(“单击”,myfunction);
函数myfunction(){
getMemberByName('nombre1','apellido1')
}

注意不要调用函数
$(“#boton2”)。在(“click”,myfunction()),因为这将在加载页面时调用函数,而不是在加载之后调用。根据我的经验。

如果这可以帮助某人,正如@Moosman所说,您需要在函数中包装您想要做的事情,并且您不能调用。例如:

<script>
    $("#boton2").on("click", function(){getMemberByName('nombre1','apellido1')});
</script>

$(“#boton2”)。在(“单击”,函数(){getMemberByName('nombre1','apellido1')});
或:


$(“#boton2”)。在(“单击”,myfunction);
函数myfunction(){
getMemberByName('nombre1','apellido1')
}
注意不要调用函数
$(“#boton2”)。在(“click”,myfunction())