将对象发送到数据库';s和API';s javascript

将对象发送到数据库';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"

您好,我正在尝试获取表单的值,将它们转换为对象,然后在页面底部显示所述对象。我最终想学习从API发送和访问。但我想先看到我的对象出现

<!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等)。你对这两个问题都有意见吗?