/
@see 获得表单里的数据(不光是表单,任何元素都可以)
@param id 表单的ID
@param type 要返回的类型: string返回用&拼接的字符串, object返回一个js对象, json表示返回json格式的字符串
@return 根据type而定,默认为string
/
function getFormData(id, type) {
if (getO(id) != null) {
var str = "";
var obj = new Object();
var json = "{";
var array = getO(id)getElementsByTagName("input");
var rdbArr = new Array();
var ckbArr = new Array();
for (var i = 0; i < arraylength; i++) {
if (array[i]name != null && array[i]name != "") {
if (array[i]type == null || array[i]type == "") {
array[i]type = "text";
}
var inType = array[i]typetoLowerCase();
if (inType == "text" || inType == "hidden" || inType == "password") {
str += array[i]name + "=" + array[i]value + "&";
obj[array[i]name] = array[i]value;
json += array[i]name + ":'" + array[i]valuereplace(/\'/g, "\"") + "',";
} else if (inType == "radio") {
if (!isArrayHave(rdbArr, array[i]name)) {
rdbArr[rdbArrlength] = array[i]name;
}
} else if (inType == "checkbox") {
if (!isArrayHave(ckbArr, array[i]name)) {
ckbArr[ckbArrlength] = array[i]name;
}
}
}
}
for (var i = 0; i < rdbArrlength; i++) {
var key = rdbArr[i], value = getRadio(rdbArr[i]);
str += key + "=" + value + "&";
obj[key] = value;
json += key + ":'" + valuereplace(/\'/g, "\"") + "',";
}
for (var i = 0; i < ckbArrlength; i++) {
var key = ckbArr[i], value = getBox(ckbArr[i]);
str += key + "=" + value + "&";
obj[key] = value;
json += key + ":'" + valuereplace(/\'/g, "\"") + "',";
}
array = getO(id)getElementsByTagName("textarea");
for (var i = 0; i < arraylength; i++) {
if (array[i]name != null && array[i]name != "") {
str += array[i]name + "=" + array[i]value + "&";
obj[array[i]name] = array[i]value;
json += array[i]name + ":'" + array[i]valuereplace(/\'/g, "\"") + "',";
}
}
array = getO(id)getElementsByTagName("select");
for (var i = 0; i < arraylength; i++) {
if (array[i]name != null && array[i]name != "") {
str += array[i]name + "=" + array[i]value + "&";
obj[array[i]name] = array[i]value;
json += array[i]name + ":'" + array[i]valuereplace(/\'/g, "\"") + "',";
}
}
str = (strlength > 0 strsubstring(0, strlength - 1) : str);
json = (jsonlength > 1 jsonsubstring(0, jsonlength - 1) : json);
json += "}";
if (type == "object") {
return obj;
} else if (type == "json") {
return json;
} else {
return str;
}
} else {
alertWin(id + "\u672a\u5b9a\u4e49");
}
return "";
}
1:你可以给table加一个id这样简单很多。也可以不加id
2:取table里input type="text 所有的值
var len = documentgetElementsByTagName("table")[0]getElementsByTagName("input")length;
var inputVal;
for(var i=0;i<len;i++){
inputVal += documentgetElementsByTagName("table")[0]getElementsByTagName("input")[i]value + "<br />";
}
alert(inputVal);
3:去select下的option的val 一样通过for循环遍历出来。这会吧。
以上就是关于获取数值的表单JS 怎么写呢全部的内容,包括:获取数值的表单JS 怎么写呢、js 获取table中表单中的值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)