从具有值的表单中删除输入元素会导致网站崩溃
Removing input element from form that has a value result in website crash
我有一个包含一系列输入字段的表单,我还用javascript动态添加输入字段到这个表单中。我正试图用以下代码动态删除这些元素:
var catchMarkers = [];
var fishingtripMarker;
var catchIdCounter = 0;
function placeCatchMarker(position)
{
catchMarker = new google.maps.Marker({
position: position,
map: map,
draggable: true,
animation: google.maps.Animation.DROP
});
catchMarker.set("id", catchIdCounter);
AddCatchInput(catchIdCounter);
catchIdCounter++;
//Right click listener
markerListener = google.maps.event.addListener(catchMarker, 'rightclick', function(event) {
//removing inputs from form
var fish = document.getElementById('Fish'+this.get("id"));
var weight = document.getElementById('Weight'+this.get("id"));
fish.parentNode.removeChild(fish);
weight.parentNode.removeChild(weight);
//removing marker
this.setMap(null);
var index = catchMarkers.indexOf(this);
catchMarkers.splice(index, 1);
});
catchMarkers.push(catchMarker);
}
我用以下代码创建这些输入字段:
function AddCatchInput($id)
{
var container = document.getElementById("myform");
var select = document.createElement("select");
select.setAttribute("id", "Fish"+$id);
var option;
option = document.createElement("option");
option.setAttribute("value", "1");
option.innerHTML = "Trout";
select.appendChild(option);
var input = document.createElement("input");
input.id = "Weight" + $id;
container.appendChild(select);
container.appendChild(input);
}
一切都很好,直到我试图删除这些输入字段时,任何表单的输入字段都有值,然后网站崩溃了,我不知道为什么。
这显然是一个与右键单击事件有关的bug(在chrome中)。
问题不在于输入有一个值,而是当你右键点击标记时,输入有焦点时,就会发生这种情况,崩溃演示:http://jsfiddle.net/doktormolle/57V72/
可能的解决方案:在右键点击触发之前将焦点从输入中移除,例如onmousedown:
google.maps.event.addListener(catchMarker, 'mousedown', function (event) {
document.getElementById('Weight' + this.get("id")).blur();
});
没有崩溃的演示:http://jsfiddle.net/doktormolle/57V72/1/
你应该报告这个错误。
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何设置html元素填充的动画
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 表追加而不附加最后一个元素
- 如何在jQuery中获取元素的形式
- 我可以获得相对于被点击元素的确切点击位置吗
- 在函数中添加数组元素的数值
- Mobile Safari (iPhone/iPad) 在动态插入大量 DOM 元素时崩溃
- 将currentTime设置为<音频>元素导致Firefox崩溃
- 访问导致我的应用程序崩溃的数组元素
- 使用twitter bootstrap防止子元素崩溃
- 如何触发只有一个元素与推特崩溃
- 元素排序例程在Firefox中工作,但在Chrome中崩溃
- jQuery IE 9在替换"tbody"时崩溃;值,单击其中的元素后
- 防止面板在标题子元素点击时崩溃
- 从具有值的表单中删除输入元素会导致网站崩溃
- IE 11“崩溃”使用动态SVG元素时