$(function () {
var info = JSON.parse($('#userInfo').text());
var empFirstName = info.EmployeedFirstName;
var empLastName = info.EmployeedLastName;
var empFullName = empFirstName + " " + empLastName;
});
我从超链接调用摘要函数。我只想在点击链接时调用摘要功能。
<a onclick="summary();">Summary</a>
function summary(empFullName){
}
答案 0 :(得分:1)
从链接中删除内联函数调用,并在链接中添加一个click事件处理程序,然后在调用click处理程序时调用该函数并将变量传递给它。看一下片段,你就会明白这个想法。
$(function () {
//var info = JSON.parse($('#userInfo').text());
//var empFirstName = info.EmployeedFirstName;
//var empLastName = info.EmployeedLastName;
var empFullName = 'foo' + " " + 'bar';
// added click event handler here
$('a').on('click', function() {
summary(empFullName); // call summary with empFullName
});
});
function summary(empFullName){
console.log(empFullName);
$('#output').text(empFullName);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="#">Summary</a>
<div id="output"></div>
&#13;
答案 1 :(得分:0)
这对您有用,但您应该查看window
对象,全局变量以及它们可能被视为错误的原因。
$(function () {
var info = JSON.parse($('#userInfo').text());
var empFirstName = info.EmployeedFirstName;
var empLastName = info.EmployeedLastName;
window.empFullName = empFirstName + " " + empLastName;
});
function summary(empFullName){
// use empFullName here
}
然后修改您的链接以传递全局
<a onclick="summary(empFullName);">Summary</a>