87,921
社区成员
发帖
与我相关
我的任务
分享
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.fn.extend({
"setJson": function (json) {
var id = this.id;
var $form = $(this);
//清空原有表单
$form.find("select").each(function () {
this.selectedIndex = 0;
});
$form.find(":hidden").val(""); //# B 必须注释这一行,换成下一行( #C ), IE8(部分电脑) 和 Chrome 后面的行( #D )才能正常运行, 为什么?
//clearHidden(this.id); //# C
$form = $("#");
//给表单赋值
$("#sel").val("1"); //# D
}
});
function clearHidden(id) {
$("#"+id).find(":hidden").val("");
}
function test() {
var o = { "sel": "1" };
$("#bd").setJson(o);
}
</script>
</head>
<body id="bd">
<input type="hidden" value="1" id="vtid" />
<select id="sel">
<option value="0">---请选择---</option>
<option value="1">第一项</option>
<option value="2">第二项</option>
</select>
<input type="button" value="test" onclick="test()" />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$.fn.extend({
"setJson": function (json) {
var id = this.id;
var $form = $(this);
//清空原有表单
$form.find("select").each(function () {
this.selectedIndex = 0;
});
$form.find("[type=hidden]").val(""); //# B 必须注释这一行,换成下一行( #C ), IE8(部分电脑) 和 Chrome 后面的行( #D )才能正常运行, 为什么?
//clearHidden(this.id); //# C
//$form = $("#");
//给表单赋值
//$("#sel").val("b"); //# D
$("#sel").prop("selectedIndex", 1);
}
});
function clearHidden(id) {
$("#"+id).find(":hidden").val("");
}
function test() {
var o = { "sel": "1" };
$("#bd").setJson(o);
}
</script>
</head>
<body id="bd">
<input type="hidden" value="1" id="vtid" />
<select id="sel">
<option value="0">---请选择---</option>
<option value="1">第一项</option>
<option value="2">第二项</option>
</select>
<input type="button" value="test" onclick="test()" />
<script>
var hiddens = $(":hidden");
for(var i = 0, len = hiddens.length; i < len; i++){
console.log(hiddens[i]);
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.fn.extend({
"setJson": function (json) {
var id = this.attr("id"); //this已经是body被包装后的jquery对象
//清空原有表单
this.find("select").each(function () {
this.selectedIndex = 0;//此处的this是循环中的每一个select , 不再是body
});
this.find(":hidden").val(""); //# B (此处的this是body被包装后的jquery对象) 必须注释这一行, IE8(部分电脑) 和 Chrome 后面的行( #C )才能正常运行, 为什么?
//给表单赋值
$("#sel").val("1"); //# C
}
});
function test() {
var o = { "sel": "1" };
$("#bd").setJson(o);
}
</script>
</head>
<body id="bd">
<input type="hidden" value="1" id="vtid" />
<select id="sel">
<option value="0">---请选择---</option>
<option value="1">第一项</option>
<option value="2">第二项</option>
</select>
<input type="button" value="test" onclick="test()" />
</body>
</html>