如何调用 jQuery 函数 onclick?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16976787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call jQuery function onclick?
提问by milano
I am using this code to get the current url of the page in to div, when I click on submit button. I am not getting how to call function onclick. How to do this?
当我单击提交按钮时,我正在使用此代码将页面的当前 url 获取到 div 中。我不知道如何调用 onclick 函数。这该怎么做?
<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>
回答by vinothini
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>');
});
回答by Tushar Gupta - curioustushar
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>');
});
});
回答by Bharat Chodvadiya
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>
回答by Zain Shaikh
try this:
尝试这个:
$('form').submit(function(){
// this function will be raised when submit button is clicked.
// perform submit operations here
});
回答by Sudarshan
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">