我想知道表單中的不同元素及其類型。jquery - 如何識別表單字段類型
我的HTML看起來像這樣:
<form action="save_form" method="post" id="newform" name="newform">
<div class="form_description">
<h2 class="editable">Form title. Click here to edit</h2>
<p class="editable">This is your form description. Click here to edit.</p>
</div>
<ul id="form_body" class="ui-sortable">
<li id="field1" class="">
<a href="#" onclick="return false;" class="hover">
<label class="editable" for="field1">Text1</label>
<input type="text" name="field1" value="">
</a>
</li>
<li id="field2" class="">
<a href="#" onclick="return false;" class="hover">
<label class="editable" for="field2">Paragraph2</label>
<div><textarea cols="" rows="" class="textarea" name="field2"></textarea></div>
</a>
</li>
<li id="radiogroup3" class="">
<a href="#" onclick="return false;" class="hover">
<label class="editable" for="radiogroup3">Label3</label>
<input type="radio" id="radiogroup31" name="radiogroup3" value="1" class="element radio">
<label class="choice editable" for="radiogroup31">option1</label>
<input type="radio" id="radiogroup32" name="radiogroup3" value="2" class="element radio">
<label class="choice editable" for="radiogroup32">option2</label>
<input type="radio" id="radiogroup33" name="radiogroup3" value="3" class="element radio">
<label class="choice editable" for="radiogroup33">option3</label>
</a>
</li></ul>
</form>
我想出了下面的功能,但this.type不返回任何值(不確定),即使 螢火控制檯顯示正確的值。
我不能使用ATTR(「類型」),因爲它沒有爲文本區域和標籤的工作。
test = $('#newform').map(function(){ return $.makeArray(this.elements); });
if (test.length != 0) {
test.each(function(){
console.log($(this).type);
});
}
有沒有一種方法可以獲取表單中的所有元素,包括標籤,textarea和單選按鈕。
你只想'輸入','標籤'和'textarea'元素,還是'form'元素的所有子元素(例如,在你的例子中包含'li'或'a'元素)? –
'$(this).type'沒有定義,你應該使用'this.type'。 – Paulpro
詹姆斯,我想獲得所有形式相關的元素,如標籤,輸入,textarea,廣播等,最好按照他們的形式 – truthSeekr