jQuery click触发两次(一次在页面加载和点击时),但是当我点击列表项时应该触发事件。
HTML:
<ul>
<li id="toolbar-save">
<i class="fa fa-fw fa-save"></i>
<span>Save</span>
</li>
</ul>
jQuery的:
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
$("#toolbar-save").click(function() {
CallValidateMethod();
});
function CallValidateMethod(Model) {
$.ajax({
url: '@Url.Action("Validate", "Customer")',
type: 'GET',
dataType: 'JSON',
cache: false,
data: { Model: Model },
}).done(function() {
alert('Added');
});
}
答案 0 :(得分:0)
你的选择器错了。
这有效:
$("li#toolbar-save").click(function () {
CallValidateMethod();
});
function CallValidateMethod(Model) {
alert("yay");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li id="toolbar-save">
<i class="fa fa-fw fa-save"></i>
<span>Save</span>
</li>
</ul>
答案 1 :(得分:0)
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#toolbar-save").click(function () {
CallValidateMethod();
});
function CallValidateMethod()
{
alert('function called')
}
});
</script>
</head>
<body>
<ul>
<li id="toolbar-save">
<i class="fa fa-fw fa-save"></i>
<span>Save</span>
</li>
</ul>
</body>
</html>