Javascript ajax上的get函数中存在未定义的错误

Javascript ajax上的get函数中存在未定义的错误,javascript,jquery,ajax,Javascript,Jquery,Ajax,你好,我的ajax代码出现了一个奇怪的问题,请看一下: $(".productDropdown").bind("change", productDropdown); function productDropdown(){ $.get('/api/productDropdown', { option: $(this).val() }, function(data) { var parent = $(this).parents('tr');

你好,我的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>

品名
描述
量
价格
行动
var my_var={”“:“请选择”,“1:”扑热息痛“,“2:”生物源“,“3:”Bioflu“,“4:”Medicol”};
请选择
扑热息痛
生物地理学
生物燃料
麦地考
任何想法请…您的帮助将非常感谢,非常感谢您的时间。

$(这)
在您的成功功能中并不是您所期望的。您需要在
$之前定义一个变量。获取
并在success函数中使用该变量:

$(".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);

    });

}

您不能在成功函数中使用
$(this)
。谢谢@putvade提供这些信息!我不知道那件事。。真的很感激!还有一个错误,为什么我不能使用'price.val(data.price);`我收到一个未捕获的TypeError:无法调用未定义的方法'val',谢谢..你是怎么做到的?也许你可以把它加到你的问题上。。这只是我的一个错误,因为我声明了price=$this.find(input.price.val();然后我在后面分配到price.val(data.price);这意味着我在price变量上分配了两个.val。。。对不起,那是我的错误。。无论如何,非常感谢你愿意帮忙!:)很好的一天!谢谢@kehrk提供的信息和快速修复!我不知道那件事。。真的很感激@用户2997398不是问题。我相信我在过去也遇到过同样的问题。仍然有一个错误,为什么我不能使用'price.val(data.price);'我得到了一个未捕获的TypeError:无法调用Undefine的方法'val'。我想我在没有看到
.productDropdown
元素的HTML及其父元素的HTML的情况下无法解决这个问题。