我試圖運行這樣的功能:爲什麼我無法將句子值傳遞給jQuery函數?
onclick="preConfirm(New Lots Avenue,40.660223,-73.896668);"
但我一直在控制檯中的錯誤說
Uncaught SyntaxError: missing) after argument list
我在做什麼錯?
我試圖運行這樣的功能:爲什麼我無法將句子值傳遞給jQuery函數?
onclick="preConfirm(New Lots Avenue,40.660223,-73.896668);"
但我一直在控制檯中的錯誤說
Uncaught SyntaxError: missing) after argument list
我在做什麼錯?
onclick="preConfirm('New Lots Avenue','40.660223','-73.896668');"
法哈德的答案是正確的,但你也可能需要j08691的評論考慮,並從你的HTML代碼中分離您的JS:
1)使用數據屬性來保存您的信息
<button
data-address="New Lots Avenue"
data-lat="40.660223"
data-lng="-73.896668"
>Confirm</button>
2 )並使用JavaScript獲取preConfirm
函數中的信息:
// Grab the button and add an event listener
const button = document.querySelector('button');
button.addEventListener('click', preConfirm, false);
function preConfirm(e) {
// dataset is an object containing your information
// { address → "New Lots Avenue", lat → "40.660223", lng → "-73.896668" }
console.log(e.target.dataset);
}
或者更簡單地使用jQuery:
$('button').click(preConfirm);
function preConfirm() {
console.log($(this).data());
}
您可以訪問DataSet對象爲正常:
dataset.address // New Lots Avenue
dataset.lat // 40.660223
dataset.lng // -73.896668
第1步:不要使用內聯事件處理程序/ JavaScript – j08691
字符串值需要用引號引起來。就像每一種編程語言一樣。 – David