简体   繁体   English

如何调用jQuery函数onclick?

[英]How to call jQuery function onclick?

I am using this code to get the current url of the page in to div, when I click on submit button. 当我点击提交按钮时,我正在使用此代码将页面的当前网址添加到div中。 I am not getting how to call function onclick. 我没有得到如何调用函数onclick。 How to do this? 这个怎么做?

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>localhost</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script type="text/javascript">
            $(function () {
                var url = $(location).attr('href');
                $('#spn_url').html('<strong>' + url + '</strong>');
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="spn_url"></div>
            <input type="submit" value="submit" name="submit">
        </form>
    </body>
</html>

Please have a look at http://jsfiddle.net/2dJAN/59/ 请看http://jsfiddle.net/2dJAN/59/

$("#submit").click(function () {
var url = $(location).attr('href');
$('#spn_url').html('<strong>' + url + '</strong>');
});

try this .. 尝试这个 ..

HTML HTML

<input type="submit" value="submit" name="submit" id="submit">

jQuery jQuery的

$(document).ready(function () {
    $('#submit').click(function () {
        var url = $(location).attr('href');
        $('#spn_url').html('<strong>' + url + '</strong>');
    });
});

Try this: 尝试这个:

HTML: HTML:

<input type="submit" value="submit" name="submit" onclick="myfunction()">

jQuery: jQuery的:

<script type="text/javascript">
 function myfunction() 
 {
    var url = $(location).attr('href');
    $('#spn_url').html('<strong>' + url + '</strong>');
 }
</script>

try this: 尝试这个:

$('form').submit(function(){
    // this function will be raised when submit button is clicked.
    // perform submit operations here
});

JS JS

 $(function () {
    var url = $(location).attr('href');
    $('#spn_url').html('<strong>' + url + '</strong>');
    $("#submit").click(function () {
        alert('button clicked');
    });
});

html HTML

<input id="submit" type="submit" value="submit" name="submit">

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

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