我有一个这样的数组:
["school_name":"My School Name", "school_number":"54546", "note":"", "class":1]
和这样的HTML:
<input type="text" name="school_name" />
<input type="text" name="school_number" />
<textarea name="note" ></textarea>
<select name="class">
<option value="1"> One </option>
<option value="2"> Two </option>
<option value="3"> Three </option>
</select>
我想从其属性名称中获取标签的名称:
$('*[name=school_name]').attr("tag"); // this is input
$('*[name=class]').attr("tag"); // this is select
$('*[name=note]').attr("tag"); // this is textarea
请注意,我已经尝试过:
$('*[name=school_name]').attr("tag");
$('*[name=school_name]').prop("tag");
$('*[name=school_name]').get(0).tagName;
$('*[name=school_name]')[0].tagName;
这就是我想要做的:
如果标签名称是输入文本,
$("input[name=school_name]").attr("value", "My School Name");
如果标签名称是一个选择框,
$("select[name=class]").children("option[value=2]").attr("selected", "selected");