我正在尝试使用jQuery添加邮件属性,但是jQuery没有被触发。
下面的是我的锚标记:
<a href="" class="sendMail">share a friend</a>
邮件部分使用以下jQuery代码构建。请不要在MVC控制器的视图文件中实现。
<script type="text/javascript">
$(function () {
$('.sendMail').click(function () {
var email = '';
var subject = '@Model.UserName' + 'has shared a snippet with you;
var emailBody = '@Model.URL' + is the url;
window.location.href = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
return false;
});
});
</script>
Model.username和Model.url是来自数据库的值 但是邮件部分没有被触发。有什么建议吗?
答案 0 :(得分:0)
您需要阻止锚点击的默认行为:
<script type="text/javascript">
$(function () {
$('.sendMail').click(function (e) {
e.preventDefault(); //<-- Note here
var email = '';
var subject = '@Model.UserName' + 'has shared a snippet with you'; //<-- Missing closing quote
var emailBody = '@Model.URL' + 'is the url';//<-- Missing opening & closing quote
window.location.href = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
// return false; //<-- no need of this
});
});
</script>