我发誓这曾经有效。我正在尝试做面包屑。我在名为“breadcrumb”的全局命名空间中有一个 JS 函数。它接受一个字符串作为其唯一参数:
function breadcrumb(attr) {
hash = { type: "POST", url: "brands/get_attributes", data: $("#attributeform").serialize() };
$.ajax(hash);
return false;
}
我在 HTML 页面上的面包屑链接定义如下:
<a href="javascript:breadcrumb(\'<%= brand_attribute.attributename %>\');"><%= value %></a>
到目前为止还不错。但是当我点击面包屑链接时,我在 URL 中得到了这个:
javascript:breadcrumb('Whirlpool');
还有一个带有“false”字样的空白页,这是函数返回的内容。我的问题是,为什么它实际上导航到 javascript 函数本身?为什么没有呈现 Ajax 结果?
最佳答案
使用 onclick 而不是 href。此外,我的强制症建议进行以下语法更改:
var hash = { //use braces to define an object
type: "POST",
url: "brands/get_attributes",
data: $("#attributeform").serialize()
};
关于javascript - JS 函数将 Javascript 返回给浏览器而不是预期的 Ajax post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13382000/