将对象发送到数据库';s和API';s javascript
您好,我正在尝试获取表单的值,将它们转换为对象,然后在页面底部显示所述对象。我最终想学习从API发送和访问。但我想先看到我的对象出现将对象发送到数据库';s和API';s javascript,javascript,html,Javascript,Html,您好,我正在尝试获取表单的值,将它们转换为对象,然后在页面底部显示所述对象。我最终想学习从API发送和访问。但我想先看到我的对象出现 <!doctype html> <html> <body> <form id="submitF" class="formClass" > <input id="inp1" type="text" /></br></br> <input id="inp2"
<!doctype html>
<html>
<body>
<form id="submitF" class="formClass" >
<input id="inp1" type="text" /></br></br>
<input id="inp2" type="text"/></br></br>
<input id="inp3" type="text"/> </br></br>
<input id="inp4" type="text" /> </br></br>
<input id="inp5" type="text"/></br></br>
<button id="btn" type="submit" OnClick=button() > Submit </button>
</form>
<script>
var myObject = new Object(); {
inp1 = document.getElementById('inp1');
inp2 = document.getElementById('inp2');
inp3 = document.getElementById('inp3');
inp4 = document.getElementById('inp4');
inp5 = document.getElementById('inp5');
}
var button = function(myObject){
for(var i = 0; i < apartment.length; i++ ){
console.log(myObject(i));
}
};
</script>
提交
var myObject=新对象();{
inp1=document.getElementById('inp1');
inp2=document.getElementById('inp2');
inp3=document.getElementById('inp3');
inp4=document.getElementById('inp4');
inp5=document.getElementById('inp5');
}
var按钮=功能(myObject){
对于(变量i=0;i
newobject()代码>,以及在之后使用对象文字语法代码>表示表达式将被忽略。将其更改为:
var myObject = {
inp1 = ...
};
document.getElementById(id)
返回一个HTMLElement
对象,而不是其中包含的值,您需要使用.value
属性:
inp1 = document.getElementById('inp1').value
inp1 = document.getElementById('inp1').value,
inp2 = document.getElementById('inp2').value,
inp3 = document.getElementById('inp3').value
onclick
(作为属性或onclick=”“
属性)。而是使用addEventListener
DOM方法:
var button = document.getElementById("btn");
button.addEventListener( 'click', function(e) {
alert("you clicked the button.")
} );
公寓
的对象,该对象不存在。我假设您的意思是myObject
,但您正在迭代整数索引,整数索引不存在,因为myObject
不是array
,而是object
(数组具有索引值,对象具有命名属性)将按钮类型从submit更改为button,并按如下方式更改脚本:
<form id="submitF" class="formClass" >
<input id="inp1" type="text" />
<input id="inp2" type="text"/>
<input id="inp3" type="text"/>
<input id="inp4" type="text" />
<input id="inp5" type="text"/>
<button id="btn" type="button" onclick="button()"> Submit </button>
</form>
<script>
var button = function(){
var myObject = {
inp1: document.getElementById('inp1').value,
inp2: document.getElementById('inp2').value,
inp3: document.getElementById('inp3').value,
inp4: document.getElementById('inp4').value,
inp5: document.getElementById('inp5').value,
}
console.log(myObject);
}
</script>
提交
var按钮=函数(){
变量myObject={
inp1:document.getElementById('inp1')。值,
inp2:document.getElementById('inp2').value,
inp3:document.getElementById('inp3')。值,
inp4:document.getElementById('inp4')。值,
inp5:document.getElementById('inp5')。值,
}
console.log(myObject);
}
您试图在页面底部显示对象的和访问API的是什么?感谢您的深入解释。我是新来的(如你所知)。我把它登录到控制台上了!现在您有什么建议可以帮助发送要存储在API中的对象吗?@cwa我不能,因为您没有告诉我们您想要使用的API(我想是web服务)的任何信息。有很多类型:例如SOAP、XML-RPC、WebSocket和REST等,还有不同的请求/响应类型(XML、JSON、表单键/值对等),更不用说身份验证技术(HTTP Basic、自定义、cookies、HMAC、OAuth等)。你对这两个问题都有意见吗?