将 HTML 值分配给 JavaScript 变量
问题描述:
我有以下 Javascript 函数允许我向表中添加新行:
I have the following Javascript function that allows me to add a new row to my table:
$(function() {
var $table = $('table.pv-data'),
counter = 1;
$('a.add-author').click(function(event){
event.preventDefault();
counter++;
var newRow =
'<tr>' +
'<td><input type="text" name="id' + counter + '"/></td>' +
'<td><select name="state' + counter + '"/><OPTION value= "persil">Persil</OPTION><OPTION value= "other">Other</OPTION></select></td> ' +
'<td><input type="text" name="longitude' + counter + '"/></td>' +
'<td><input type="text" name="latitude' + counter + '"/></td>' +
'<td><input type="text" name="altitude' + counter + '"/></td>' +
'<td><input type="text" name="module_tilt' + counter + '"/></td>' +
'<td><a href="#" class="remove">remove</a></td>'
'</tr>';
$table.append(newRow);
});
});
一切正常,只是我必须在选项之间进行选择的部分:Persil"或其他"不起作用.它没有显示不同的选项.
Everything is working fine, just the part where I have to select between the options:"Persil" or "Other" is not working. it's not showing the different options.
答
以下几行需要替换
'
...'.不正确,因为您过早关闭了 select 标签. '<td><a href="#" class="remove">remove</a></td>'
.您在末尾缺少+
.-
'<td><select name="state' + counter + '"/>...'
. Is not correct as you are closing the select tag too early. -
'<td><a href="#" class="remove">remove</a></td>'
. You are missing+
at the end.
通过这些替换:
'