获取数值的表单JS 怎么写呢

获取数值的表单JS 怎么写呢,第1张

/

@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 "";

}

<form name="form1" action=">

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 401 Transitional//EN">

<html>

<head>

<meta >

以上就是关于获取数值的表单JS 怎么写呢全部的内容,包括:获取数值的表单JS 怎么写呢、javascript中dom 如何获取表单字段数据、js获取表单数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9826451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存