[英]Why function variable (when logged in console) does not show the whole function body?
[英]Why does a data value logged to the console work, but not when assigned to a variable?
Javascript 新手在這里。 嘗試設置新的 Stripe checkout V3。 我需要在 Rails 中從 HTML 獲取會話 ID 到我的 Javascript。
HTML文件:
<%= content_tag :div, id: "temp_information", data: {temp: @session.id} do %>
<% end %>
<div data-stripe="<%= @session.id %>">
<button id="checkout-button">Pay</button>
</div>
Javascript文件:
var ses_id = $('#temp_information').data('temp');
alert(ses_id);
stripe.redirectToCheckout({
// Make the id field from the Checkout Session creation API response
// available to this file, so you can provide it as parameter here
// instead of the {{CHECKOUT_SESSION_ID}} placeholder.
sessionId: ses_id
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
});
我對 ses_id 未定義。 當我做
$('#temp_information').data('temp');
在控制台中,它顯示了正確的輸出。 但是當我這樣做時:
var a = $('#temp_information').data('temp');
然后我在控制台中未定義。 我在這里做錯了什么? 謝謝你的幫助!
$.data(parameter) 用於存儲信息, $.data() 用於獲取存儲的所有數據,因此您只需要刪除 'temp 即可獲取數據。
var a = $('#temp_information').data();
參考: https : //api.jquery.com/data/
原來我需要使用:
$( document ).ready(function() {
*my code*
)};
在嘗試訪問值之前等待所有內容加載。 我認為這是因為 Rails 或 Turbolinks。 不完全確定哪個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.