ajax上get函数内部的未定义错误
undefined error inside the get function on ajax
你好,我的ajax代码出现了这个奇怪的问题,请看一下:
$(".productDropdown").bind("change", productDropdown);
function productDropdown(){
$.get('/api/productDropdown',
{ option: $(this).val() },
function(data) {
var parent = $(this).parents('tr');
var qty = $(parent).find('input.qty').val();
var price = $(parent).find('input.price').val();
var description = $(parent).find('textarea.description').val();
console.log(price+":"+qty+":"+description);
});
}
日志结果为:
undefined:undefined:undefined
但是当我在get函数之外声明变量时,我可以得到正确的值。但我仍然无法在文本框中分配它们。因为当我尝试投放时:
description.val(data.description);
在变量下面,我得到了这个错误:
Uncaught TypeError: Cannot call method 'val' of undefined
(这是一个外部js文件)
(更新)
这是我的HTML文件:
<table id="tblContacts" class="table-responsive table table-striped table-bordered tablesorter table-hover col-lg-12">
<thead>
<tr>
<th class="col-lg-3">Product Name</th>
<th class="col-lg-4">Description</th>
<th class="col-lg-1">Quantity</th>
<th class="col-lg-1">Price</th>
<th class="col-lg-3">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<script type="text/javascript">
var my_var = {"":"Please select","1":"Paracetamol","2":"Biogesic","3":"Bioflu","4":"Medicol"};
</script>
<select class="form-control productDropdown" placeholder="Company Name" id="productId" required="required" name="product[0][name]">
<option value="" selected="selected">Please select</option>
<option value="1">Paracetamol</option>
<option value="2">Biogesic</option>
<option value="3">Bioflu</option>
<option value="4">Medicol</option>
</select>
</td>
<td><textarea name='product[0][description]' class="form-control description" rows="1" required></textarea>
</td>
<td><input type='number' min="1" max="9999" name='product[0][quantity]' class="form-control qty" required value="52"></td>
<td><input type='number' min="0.5" max="9999" name='product[0][price]' class="form-control price" required/></td>
<td>
<a href="#" class="btnRemoveRow btn btn-danger">Remove</a>
</td>
</tr>
</tbody>
</table>
有什么想法吗。。。非常感谢您的帮助,非常感谢您抽出时间。
成功函数中的$(this)
并不是您所期望的。您需要在$.get
之前定义一个变量,并在成功函数中使用该变量:
$(".productDropdown").bind("change", productDropdown);
function productDropdown() {
var target = $(this);
$.get('/api/productDropdown', {
option: $(this).val()
}, function (data) {
var parent = target.parents('tr');
var qty = $(parent).find('input.qty').val();
var price = $(parent).find('input.price').val();
var description = $(parent).find('textarea.description').val();
console.log(price + ":" + qty + ":" + description);
});
}
试试这个:
$(".productDropdown").bind("change", productDropdown);
function productDropdown(){
var $this = $(this);
$.get('/api/productDropdown',
{ option: $this.val() },
function(data) {
var parent = $this.parents('tr');
var qty = $(parent).find('input.qty').val();
var price = $(parent).find('input.price').val();
var description = $(parent).find('textarea.description').val();
console.log(price+":"+qty+":"+description);
});
}
相关文章:
- chrome.storage.sync未定义错误
- 对象为null或IE9中存在未定义错误
- 已定义变量出现未定义错误
- 'define' 在 RequireJS 和 Webapp Yo 生成器上未定义错误
- onreadystatechange未定义错误
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 如何修复JSlint报告的“foo未定义”错误
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 主干无法读取属性'属性'主干视图中的未定义错误
- 如何克服javascript中的未定义错误
- require未定义错误
- 单击链接时出现函数未定义错误
- 函数为未定义错误
- 主干未定义错误
- 使用Javascript打印HTML:未定义错误
- 避免“无法读取”属性'获取'的未定义错误
- 未捕获类型错误的coffescription:无法读取属性'offsetWidth'的未定义错误
- ajax上get函数内部的未定义错误
- Javascript变量未定义错误
- AngularJS指令作用域未解析(“属性名称未定义”错误)