var i = 0;
function addLine() {
i++;
var div = document.createElement('div');
div.innerHTML += '<input type="text" name="description['+i+']" id="description['+i+']"/>';
div.innerHTML += '<input type="text" name="quantity['+i+']" id="quantity['+i+']" onkeyup="sum();"/>';
div.innerHTML += '<input type="text" name="price['+i+']" id="price['+i+']" onkeyup="sum();"/>';
div.innerHTML += '<input type="text" name="lineTotal['+i+']" id="lineTotal['+i+']" readonly/>';
div.innerHTML += '<input type="button" onclick="removeLine(this)" value="-">';
div.innerHTML += '<input type="button" onclick="addLine();" value="+" />';
document.getElementById('items').appendChild(div);
}
function removeLine(div) {
document.getElementById('items').removeChild(div.parentNode);
}
function sum() {
var q = document.getElementById('quantity').value;
var p = document.getElementById('price').value;
var result = parseInt(q) * parseInt(p);
if(q=="" || p==""){
document.getElementById('lineTotal').value = 0;
}
if (!isNaN(result)) {
document.getElementById('lineTotal').value = result;
}
}
</script>
下面是html
<tr>
<td><input type="text" name="description[]"/></td>
<td><input type="text" name="quantity[]" id="quantity[]" onkeyup="sum();"/></td>
<td><input type="text" name="price[]" id="price[]" onkeyup="sum();"/></td>
<td><input type="text" name="lineTotal[]" id="lineTotal[]" readonly /></td>
<td><input type="button" onclick="addLine();" value="+" /></td>
</tr>
因此,当我尝试添加新行时,它可以很好地运行,没有任何问题。
当我对html中的第一个进行计算时,它完成所有计算而没有任何问题,但是当我添加新的动态线时,我无法计算。一直试图解决这个问题,仍然没有得到结果所以我决定在这里问。
非常感谢您的帮助。
非常感谢:)
答案 0 :(得分:0)
ID应该是唯一的
此外,我用TR替换了DIV并添加了TD以与您拥有的表格保持一致
DEFAULT_WALL='Public'
def wall_key(wall_name=DEFAULT_WALL):
return ndb.Key('Wall', wall_name)
class Post(ndb.Model):
guest_name=ndb.StringProperty(indexed=False)
guest_message=ndb.StringProperty(indexed=False)
date=ndb.DateTimeProperty(auto_now_add=True)
class guestbookHandler(myHandler):
def get(self):
wall_name = self.request.get('wall_name',DEFAULT_WALL)
posts_query = Post.query(ancestor = wall_key(wall_name)).order(-Post.date)
posts=posts_query.fetch(20)
template_values={
'title': 'Intro To {{Programming}}',
}
template=jinja_env.get_template('guestbook.html')
guest_name=self.request.get_all("guest_name")
guest_message=self.request.get_all("guest_message")
self.response.out.write(template.render(template_values, guest_name=guest_name, guest_message=guest_message))
def post(self):
wall_name = self.request.get('wall_name',DEFAULT_WALL)
post = Post(parent=wall_key(wall_name))
guest_message = self.request.get('guest_message')
if type(guest_message) != unicode:
post.guest_message = unicode(self.request.get('guest_message'),'utf-8')
else:
post.guest_message = self.request.get('guest_message')
post.put()
query_params = {'wall_name': wall_name}
self.redirect('/guestbook.html?' + urllib.urlencode(query_params))