У меня есть следующий код HTML и хотел бы вернуть значение SPAN обратно в 0. Textarea и текстовое поле работают нормально.
HTML
<div id="Match">FULL MATCH: <span class="fullMatch">0</span></div>
<input type="text" id="c-match" value="" />
JS
jQuery('.c-reset').on('click', function() {
jQuery('span').text(''); //this only removes the values without reverting back to 0
jQuery('textarea, #c-match').val(function() {
return this.defaultValue;
});
});
Любая помощь будет высоко ценится. благодаря
попробуйте html()
вместо text()
:
jQuery('span').html('0');
Update: span не имеет значения по умолчанию, вам нужно вручную установить его содержимое.
jQuery('span').text('0');
также будет работать
Есть несколько способов сделать это. Я предлагаю два, которые я лично считаю лучшими:
jQuery.ready(function(){//ready function
var spanDefault = jQuery('#Match span').text();
jQuery('.c-reset').on('click', function() {
jQuery('textarea, #c-match').val(function() {
setTimetou(function(){
jQuery('#Match span').text(spanDefault);
}, 1);
return this.defaultValue;
});
});
});
Установите в <span>
значение атрибута data-value="0"
, см.
<div id="Match">FULL MATCH: <span data-value="0" class="fullMatch">0</span></div>
<input type="text" id="c-match" value="" />
JS:
jQuery('.c-reset').on('click', function() {
var fullMatch = jQuery('#c-match span');
fullMatch.text(fullMatch.data("value"));
jQuery('textarea, #c-match').val(function() {
return this.defaultValue;
});
});
jQuery('span').text('0');
?$("#Match span").html(this.defaultValue); return this.defaultValue;
?