我在我的表单中使用 Bootstrap WYSIHTML5 html 编辑器,例如
<input type="text" name="product" id="product">
<textarea name="description" id="description" class="textarea" placeholder="Place some text here" style="width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;">
</textarea>
<input type="product_code" name="product_code" id="product_code">
并在输入字段中输入产品并希望使用描述更新 textarea 时使用 Ajax 获取产品详细信息
<script type="text/javascript">
$(document).ready(function($){
$('#product').autocomplete({
source:'<?php echo Router::url(array("controller" => "Products", "action" => "search")); ?>',
minLength: 1
});
$('#product').blur(function() {
var p_term = $(this).val();
if (p_term) {
var dataString = 'term='+ p_term;
//alert(dataString);
$.ajax({
dataType:'json',
type: "POST",
url: '/products/ajax-search-product',
data: dataString,
cache: false,
success: function(data) {
/* set values to input field */
$('#description').val(data['description']);
$('#product_code').val(data['product_code']);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
});
});
</script>
但是此代码在$('#product_code').val(data['product_code']);
正常工作时不会更新 textarea 字段的值。
我试图打印 textarea 的值,alert($('#description').val());
它正在工作并在 textarea 字段中打印预填充的值。
编辑 2
控制台输出:console.log(data);
Object {
id: 1,
product_code: "765457",
SKU: "",
title: "yuphoria screen guard",
description: "yuphoria screen guard",
short_description: "yuphoria screen guard",
15 more…
}
我试过这个
$('#description').val(data['description']); // this not working
alert(data['description']); // this working
只有一个id="description"
,没有其他地方使用。