我有一些像这样的html元素:
<table id="myTable"></table>
<select name="mySelect">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<a href="javascript:void(0)" onclick="addToTable()">Add new</a>
<script>
addToTable = function() {
var selected = $("select[name*='mySelect'] option:selected").val();
$('#myTable').find('tr').each(function() {
if ($(this).attr('id')==selected) {
alert('Record has already existed!'); return false;
}
else $('#myTable').append('<tr id="'+selected+'"><td>'+selected+'</td></tr>');
});
}
</script>
问题是:当我添加两个具有相同 id 的记录(行)时,它会提醒消息,但会继续追加新行而不是中断循环。我在这里做错了什么?
提前致谢。