将HTML表单输入传递给JavaScript到BaaS

时间:2015-06-03 22:30:26

标签: javascript jquery html

我正在尝试将用户HTML输入字段值存储在BaaS(Backendless)中。使用.js脚本中的固定值并在页面加载上执行它,我能够成功存储字段(apiEndpointapiSecretkeyemailAddressRequest表:

function Request(args) {
    args = args || {};
    this.apiEndpoint = args.apiEndpoint || "";
    this.apiSecretkey = args.apiSecretkey || "";
    this.emailAddress = args.emailAddress || "";
}

var requestObject = new Request( {
apiEndpoint: "https://api.foo.com/",
apiSecretkey: "foo",
emailAddress: "foo@foo.com",
});

var savedRequest = Backendless.Persistence.of( Request ).save( requestObject );

我想要做的是存储变量(并按下按钮而不是页面加载)。这是我非常基本的HTML表单:

<head>
    <script src="libs/backendless.js"></script>
    <script src="js/app.js"></script>
</head>

<body>
    <form>
        <input type="text" name="endpoint"><br>
        <input type="text" name="secretkey"><br>
        <input type="text" name="email"><br>
        <input type="button" onclick="________" value="Submit">
    </form>
</body>
带有变量的

app.js

function Request(args) {
    args = args || {};
    this.apiEndpoint = args.apiEndpoint || "";
    this.apiSecretkey = args.apiSecretkey || "";
    this.emailAddress = args.emailAddress || "";
}

var endpoint = document.getElementsByName("endpoint").value,
    secretkey = document.getElementsByName("secretkey").value,
    email = document.getElementsByName("email").value;

var requestObject = new Request( {
apiEndpoint: endpoint,
apiSecretkey: secretkey,
emailAddress: email,
});

var savedRequest = Backendless.Persistence.of( Request ).save( requestObject );

点击“提交”按钮后没有任何反应。对于表单中的onclick="",我尝试了Request()JavaScript: Request()Backendless.Persistence.of( Request ).save( requestObject );

我做错了什么?感谢帮助。

1 个答案:

答案 0 :(得分:1)

如果您粘贴的JavaScript是页面上唯一的JavaScript,那么它很可能在页面加载时全部执行(这就是硬编码值工作的原因,它们存在于加载时)。您想要的是在单击提交按钮时执行代码。您可以通过为onsubmit属性赋值,然后在JavaScript中创建一个名为sendData的函数来实现。

修改

通过忘记包含对您的BaaS的实际调用,我看起来搞砸了这个功能。这是一个片段,展示了它应该如何工作。您绝对不希望将onclick处理程序分配给您的提交按钮。

此外,getElementsByName返回NodeList而不是Element,因此未定义value属性,您必须遍历列表或访问第一个元件。如果您不介意使用ID,getElementById会更好。

function Request(args) {
    args = args || {};
    this.apiEndpoint = args.apiEndpoint || "";
    this.apiSecretkey = args.apiSecretkey || "";
    this.emailAddress = args.emailAddress || "";
}


function sendData () {
  var endpoint = document.getElementById("endpoint").value,
    secretkey = document.getElementById("secretkey").value,
    email = document.getElementById("email").value;

  var requestObject = new Request( {
    apiEndpoint: endpoint,
    apiSecretkey: secretkey,
    emailAddress: email,
  });
  alert('apiEndpoint = ' + requestObject.apiEndpoint + '\n' +'apiSecretkey = ' + requestObject.apiSecretkey + '\n' + 'emailAddress = ' + requestObject.emailAddress);
  //var savedRequest = Backendless.Persistence.of( Request ).save( requestObject );
}
<body>
    <form onsubmit="sendData()">
        <input type="text" id="endpoint"><br>
        <input type="text" id="secretkey"><br>
        <input type="text" id="email"><br>
        <input type="submit" value="Submit">
    </form>
</body>