我有一些代码可以从表单中的每个孩子那里找到“标题”属性。
当我运行'console.log('title')时,它会正确地提取标题。但是,当我尝试应用代码在字段集的内部 div 之前插入标签时,它只会为它们中的每一个添加相同的标题(“关于我”)。
html
<form action="#" method="post">
<fieldset title="About Me">
<!-- Going to convert legends to h4 // can choose the header style element? -->
<div>
<label for="name">Text Input:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />
</div>
</fieldset>
<fieldset title="Radio Button Choice">
<div>
<label for="radio-choice-1">Choice 1</label>
<input type="radio" name="radio-choice-1" id="radio-choice-1" tabindex="2" value="choice-1" />
<label for="radio-choice-2">Choice 2</label>
<input type="radio" name="radio-choice-2" id="radio-choice-2" tabindex="3" value="choice-2" />
</div>
</fieldset>
<fieldset>
<div>
<label for="select-choice">Select Dropdown Choice:</label>
<select name="select-choice" id="select-choice">
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
</div>
</fieldset>
</form>
jQ
kids = this.element.children('fieldset');
kids.each(function(){ //function to do something to each of the child fieldset elements
console.log(this);
title = $(this).attr('title');
console.log(title); //this logs each title fine, or 'undefined' where there isn't one
$("<legend>" + title + "</legend>").insertBefore('div:first-child')
//that's where I'm just getting 'About me', on every damn one....
});
谁能发现我在哪里是个傻瓜?谢谢。