對於非常混亂的代碼抱歉。我很困惑我的$().ready(function()
和個人功能應放置在哪裏。使用EventListener和jQuery添加和刪除<tr>
什麼工作: 1)$(".calc").change(function()
- 這將更新一個跨度的div從數據庫 2)當頁面加載填入表中選擇數據時,$(".calc").change(function()
調用的函數addRow(e)
之前設法寫「哎「到控制檯日誌,但以ReferenceError: Can't find variable: tbody addRow (furniture-product.php, line 396)
失敗。一旦$(".calc").change(function()
運行時,控制檯日誌不再工作
什麼不工作: 1)功能addRow(e)
2)功能removeRow(e)
我敢肯定這是有錯了地方小東西的情況下, ,但我已經嘗試了腳本的移動部分,但沒有成功。
$().ready(function() {
"use strict";
var tbody = document.getElementById("the-tbody");
document.getElementById("btn-add").addEventListener("click", addRow, false);
tbody.addEventListener("click", removeRow, false);
var radios = $('input[type=radio]');
radios.on('change', function() {
radios.each(function() {
var radio = $(this);
radio.closest('.article-option')[radio.is(':checked') ? 'addClass' : 'removeClass']('highlight');
});
});
$(".calc").change(function() {
calctotal()
});
});
function addRow(e) {
console.log("hey");
var product_id = $('input[name="product_name"]').val();
var product_price = $('input[name="product_price"]').val();
var row = document.createElement('tr');
row.innerHTML = '<td><input type="hidden" name="item_id[]" value="' + product_id + '"><p>' + name + '</p><input type="hidden" name="price[]" value="' + product_price + '" class="price">£<span id="amount" class="amount">0</span> <span class="remove">[x]</span></td>';
tbody.appendChild(row);
update_amounts();
}
function removeRow(e) {
var elm;
for (elm = e.target; elm !== this; elm = elm.parentNode) {
if (/\bremove\b/.test(elm.className)) {
removeElement(elm.parentNode);
e.stopPropagation();
return;
update_amounts();
}
}
}
function calctotal() {
var total = 0;
var radios = $('input[type=radio]');
radios.each(function() {
var radio = $(this);
radio.closest('.article-option')[radio.is(':checked') ? 'addClass' : 'removeClass']('highlight');
});
$(".calc:checked").each(function() {
var a = this.value.split(",");
total += parseFloat(a[0]);
name = (a[1]);
image = (a[2]);
curtainid = (a[3]);
curtaindesc = (a[4]);
$("span.img").html('<div class="row curtain-img"><img src="images/furniture/' + image + '" class="img-responsive img-rounded" style="border: 5px solid #5f4865"></div>');
$("div#product_name").html('<input type="hidden" name="product_name" value="' + curtainid + '">' + name + '');
$("div#product_desc").html('' + curtaindesc + '');
$("div#product_add").html('<input type="button" id="btn-add" value="Add">');
$("div#product_price").html('<input type="hidden" name="product_price" value="' + total.toFixed(2) + '"><strong>£' + total.toFixed(2) + '</strong>');
});
}
很簡單的HTML是
<table id="myTable">
<tbody id="the-tbody"></tbody>
</table>
<input type="button" id="btn-add" value="Add">
JS小提琴:http://jsfiddle.net/pw25p3qt/
某些方面很難形象化。你能製作一個[jsfiddle](http://jsfiddle.net/)嗎? – 2014-09-30 11:30:08
小提琴創建:http://jsfiddle.net/pw25p3qt/ – tomantford 2014-09-30 11:35:26
$未定義:錯誤即將到來。請添加jquery庫的參考。 – 2014-09-30 11:45:02