Javascript 建立错误列表
我正在建立一个错误列表。单击提交按钮时,将删除旧错误并添加新错误。下面的代码是我正在使用的,但它不断添加新的错误,而没有删除旧的错误:(我的JS fiddle在这里:)Javascript 建立错误列表,javascript,jquery,html,Javascript,Jquery,Html,我正在建立一个错误列表。单击提交按钮时,将删除旧错误并添加新错误。下面的代码是我正在使用的,但它不断添加新的错误,而没有删除旧的错误:(我的JS fiddle在这里:) 以下是错误列表: $(文档).ready(函数(){ $index=0; $(文档)。在('click','#submitBtn',函数(事件){ $('#error')。innerHTML=''; $('#error')。追加('error'+$index+''); $index++; }); }); 我想要实现的是,每次
以下是错误列表:
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error')。innerHTML='';
$('#error')。追加('error'+$index+'');
$index++;
});
});
我想要实现的是,每次单击按钮时,旧错误消失,新错误出现。例如,第一次单击“提交”按钮时,将显示“错误0”。第二次单击时,“错误0”消失,显示“错误1”。试试这个
$(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error p')。隐藏(“slow”);
$('#error p').html(“”);
$('#error p').html('error'+$index+'');
$('错误p')。显示(“慢”);
$index++;
});
});代码>
以下是错误列表:
试试这个
$(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error p')。隐藏(“slow”);
$('#error p').html(“”);
$('#error p').html('error'+$index+'');
$('错误p')。显示(“慢”);
$index++;
});
});代码>
以下是错误列表:
试试这个
$(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error p')。隐藏(“slow”);
$('#error p').html(“”);
$('#error p').html('error'+$index+'');
$('错误p')。显示(“慢”);
$index++;
});
});代码>
以下是错误列表:
试试这个
$(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error p')。隐藏(“slow”);
$('#error p').html(“”);
$('#error p').html('error'+$index+'');
$('错误p')。显示(“慢”);
$index++;
});
});代码>
以下是错误列表:
您正在设置innerHTML
,它不是jQuery的属性,而是本机DOM。而是使用empty
清空$(“#错误”)
。其他选项有.text(“”)
和.html(“”)
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').empty().append('error'+$index+'');
$index++;
});
});
您也可以使用indexer获取本机元素,然后设置innerHTML
,但是为什么不使用jQuery在使用时提供的东西呢?您正在设置innerHTML
,它不是jQuery的属性,而是本机DOM。而是使用empty
清空$(“#错误”)
。其他选项有.text(“”)
和.html(“”)
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').empty().append('error'+$index+'');
$index++;
});
});
您也可以使用indexer获取本机元素,然后设置innerHTML
,但是为什么不使用jQuery在使用时提供的东西呢?您正在设置innerHTML
,它不是jQuery的属性,而是本机DOM。而是使用empty
清空$(“#错误”)
。其他选项有.text(“”)
和.html(“”)
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').empty().append('error'+$index+'');
$index++;
});
});
您也可以使用indexer获取本机元素,然后设置innerHTML
,但是为什么不使用jQuery在使用时提供的东西呢?您正在设置innerHTML
,它不是jQuery的属性,而是本机DOM。而是使用empty
清空$(“#错误”)
。其他选项有.text(“”)
和.html(“”)
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').empty().append('error'+$index+'');
$index++;
});
});
您也可以使用indexer获取本机元素,然后设置innerHTML
,但为什么不使用jQuery在使用时提供的东西呢?您可以这样替换html,而不是追加:
$(document).ready(function(){
$index = 0;
$(document).on('click', '#submitBtn', function(event) {
$('#error').html('<p>Error ' + $index + '</p>');
$index ++;
});
});
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').html('error'+$index+'');
$index++;
});
});
您可以这样替换html,而不是附加:
$(document).ready(function(){
$index = 0;
$(document).on('click', '#submitBtn', function(event) {
$('#error').html('<p>Error ' + $index + '</p>');
$index ++;
});
});
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').html('error'+$index+'');
$index++;
});
});
您可以这样替换html,而不是附加:
$(document).ready(function(){
$index = 0;
$(document).on('click', '#submitBtn', function(event) {
$('#error').html('<p>Error ' + $index + '</p>');
$index ++;
});
});
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').html('error'+$index+'');
$index++;
});
});
您可以这样替换html,而不是附加:
$(document).ready(function(){
$index = 0;
$(document).on('click', '#submitBtn', function(event) {
$('#error').html('<p>Error ' + $index + '</p>');
$index ++;
});
});
$(文档).ready(函数(){
$index=0;
$(文档)。在('click','#submitBtn',函数(事件){
$('#error').html('error'+$index+'');
$index++;
});
});