我需要一些帮助来理解一些 JQUery 代码,尝试过谷歌搜索,但没有找到任何东西,而且我不知道到底要谷歌什么!(可能我所指的行可能是一些 JQuery 扩展)。
JSP
<div id="sugar-opts">
<div class="howmanysugars">
<label>How many <span id="sugar-label">sugars</span>?
:
<div class="numSugarsDiv">
<input type="text" name="num-sugars" id="num-sugars" placeholder="?" value="1" />
<button class="btn minus">-</button>
<button class="btn plus">+</button>
</div>
:
<div>
<label class="clearfix sugarLabel" >Include sugar? </label>
<label><input type="radio" name="include-sugar" value="yes" /> Yes</label>
<label><input type="radio" name="include-sugar" value="no" /> No</label>
</div>
Javascript
$sugarOptsContainer = $('#sugar-opts');
Event handler:
$('input[name=include-sugar]', $sugarOptsContainer) // first part refers to <input element,
// but what is the purpose of: , $sugarOptsContainer
.data('root', $('> :last-child', $sugarOptsContainer)) // this defines jquery object 'root' but what is the meaning
// of > :last-child, and again purpose of , $sugarOptsContainer
.on('change', handleSugarChange); // defines function called on onChange event
function handleSugarChange(event) {
var $root = $(this).data('root'); // this refer to 'root' defined above
if ($('.radio.selected', $root).length) { // checks if radio is selected, but why $root, and
// what is purpose of .length here?
$root.removeClass('error');
formData.sugar = this.value;
return;
} else {
$root.addClass('error');
}
displayMessage();
}