我试图discount_code从对象内引用属性,但我不断收到以下错误.我怎样才能访问discount_code?
错误:
未捕获的TypeError:无法调用未定义的方法'val'
HTML:
<input type="text" name="txt_discount_code" value="12345" />
Run Code Online (Sandbox Code Playgroud)
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()
}
};
Run Code Online (Sandbox Code Playgroud)
如上所述ShankarSangoli,您无法在定义之前访问该对象.
你必须将声明cart分为两部分:
var cart = {
transaction: {},
discount_code: $('input[name=txt_discount_code]')
};
cart.get_cart_items_params = {
page: 'checkout',
s_method: 'get-cart-items',
txt_discount_code: cart.discount_code.val()
};
Run Code Online (Sandbox Code Playgroud)
或者只是放入discount_code一个变量:
var $discount_code = $('input[name=txt_discount_code]');
var cart = {
transaction: {},
discount_code: $discount_code
get_cart_items_params = {
page: 'checkout',
s_method: 'get-cart-items',
txt_discount_code: $discount_code.val()
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66 次 |
| 最近记录: |