我试图从对象内引用discount_code
属性,但我不断收到以下错误。我如何访问discount_code
?物业范围问题?
错误:
Uncaught TypeError: Cannot call method 'val' of undefined
HTML:
<input type="text" name="txt_discount_code" value="12345" />
JS:
var cart = {
transaction: {},
discount_code: $('input[name=txt_discount_code]'),
get_cart_items_params: {
page: 'checkout',
s_method: 'get-cart-items',
txt_discount_code: this.discount_code.val()
// txt_discount_code: cart.discount_code.val()
}
};
这在我看来,'$(“输入[名称= txt_discount_code]')'不起作用。也许你应该从那里调试。 – hafichuk
如果我尝试用'alert($('input [name = txt_discount_code]')。val());'给它加上正确的结果,所以我不确定为什么它不起作用前面的上下文。 – Donnie
只是用选择器替换你的'this.discount_code.val()'。 – hafichuk