我的OrderItem域中有一个文本字段Price。
价格应根据所选产品自动更改/自动填充
如何使用Jquery进行此操作?到目前为止,我所做的是
<script type="text/javascript">
function loadProduct(id) {
$.ajax({
type: "POST",
url: "${resource(dir:'product/get')}/" + id,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (result) {
var div = $('#posthere')[0];
if (result.error)
alert(result.error.message);
else {
$('#price')[0].value = result.product.price;
compute();
}
//$('#dialog').dialog('open');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
var div = $('#posthere')[0];
div.innerHTML = textStatus + " " + errorThrown;
$('#dialog').dialog('open');
}
});
}
function compute(){
var price = parseFloat($('#price')[0].value);
var quantity = parseInt($('#quantity')[0].value);
$('#totalAmount')[0].value = price * quantity
}
$(function(){ // on load
var price = $('#price');
price.on('change', function() {
compute();
});
$('#quantity').on('change', function() {
compute();
});
$('#product').on('change', function() {
loadProduct($('#product')[0].value);
});
if (price[0].value == '')
loadProduct($('#price')[0].value);
else
compute();
});
</script>
我不确定上面是否有错误,请帮我解决?计算功能正常,但每当我选择产品时,价格字段都不会改变
OrderItem域名:
int orderNumber
int quantity = 1
Customer customers
Product product
BigDecimal price
BigDecimal totalAmount = 0
static constraints = {
orderNumber min:1, blank: false
quantity min: 1, blank: false
price min: 1.00, scale: 2, maxSize: 19 , blank: false
customers()
product()
totalAmount min:0.00, blank: false
}
static mapping = {
totalAmount formula: 'QUANTITY * PRICE'
}
String toString() {
return orderNumber
}
产品领域:
static hasMany = [orderitem: OrderItem]
String productCode
String productName
int quantityInStock
BigDecimal price
static constraints = {
productCode blank: false, unique: true
productName blank: false
quantityInStock blank: false, min: 1
price blank: false, min: 0.01
}
public String toString(){
return productName
}
我在这里错过了什么吗?任何帮助将不胜感激。谢谢
OST http://localhost:9001/static/product/get/ 405 (Method Not Allowed)
send @ jquery-2.1.3.js?compile=false:8625
ajax @ jquery-2.1.3.js?compile=false:8161
loadProduct @ create:43
(anonymous function) @ create:94
i @ jquery-2.2.0.min.js?compile=false:2
fireWith @ jquery-2.2.0.min.js?compile=false:2
ready @ jquery-2.2.0.min.js?compile=false:2
J @ jquery-2.2.0.min.js?compile=false:2
create:72
Uncaught TypeError: Cannot set property 'innerHTML' of undefined
error @ create:72
fire @ jquery-2.1.3.js?compile=false:3094
fireWith @ jquery-2.1.3.js?compile=false:3206
done @ jquery-2.1.3.js?compile=false:8261
(anonymous function) @ jquery-2.1.3.js?compile=false:8600
以上代码适用于谷歌开发者工具中输出的控制台错误
答案 0 :(得分:0)
这个位看起来不对,你没有在它周围放置足够的括号
if (result.error)
alert(result.error.message);
else {
$('#price')[0].value = result.product.price;
compute();
}
if (result.error) {
alert(result.error.message);
} else {
console.log('---we have '+result.product.price);
$('#price')[0].value = result.product.price;
compute();
}
您的浏览器(Chrome)上的开发人员控制台是您最好的朋友 - 该位应该抛出某种形式的java脚本错误
我添加了一个console.log,如果您使用更新的代码
进入该段,则应显示该内容