简体   繁体   English

HTML 选择下拉 ID 未传递给 Javascript 函数 getelementbyid 值为空

[英]HTML Select Dropdown ID not passing to Javascript Function getelementbyid value is null

Novice here, trying to demonstrate the value of client side rest operations to teammates as they rely on scripts and python.新手在这里,尝试向依赖脚本和python的队友展示客户端休息操作的价值。 Not sure if it's the fact I'm using numbers for the value attribute or if I'm referencing the getelementbyid wrong.不确定是因为我使用数字作为 value 属性,还是我引用了 getelementbyid 错误。 It's honestly probably none of those things :)老实说,这些东西可能都不是:)

I've tried to explicitly reference an id assigned to the IP address in the getelementbyid for MakeUrl but it still fails.我试图在 MakeUrl 的 getelementbyid 中明确引用分配给 IP 地址的 ID,但它仍然失败。 I also thought for a second it was null becasue it was using the default value but I commented it out and I still got the error.我还以为它是空的,因为它使用的是默认值,但我将其注释掉了,但仍然出现错误。

Error:错误:

Uncaught TypeError: Cannot read property 'value' of null (MakUrl Function)未捕获的类型错误:无法读取 null 的属性“值”(MakUrl 函数)

function makeUrl() {
  var ip = document.getElementById("apigatewayip").value;
  return "https://" + ip + "/api/v0/sessions"
};

function createSession(_url) {
  return $.ajax({
    "url": _url,
    "method": "POST",
    "timeout": 0,
    "headers": {
      "Content-Type": "application/json"
    },
    "data": JSON.stringify({
      "gatewayVersion": "1.00.1.15"
    }),
  });
}

createSession(makeUrl());

createSession(makeUrl()).success(function() {

});
<h1>Making AJAX Rest Calls</h1>
<div class="custom-select" style="width:200px;">
  <select name="selectip" id="apigatewayip" onchange="makeUrl">
    <option value="" disabled selected>API Gateway IP</option>
    <option value="169.254.1.10">169.254.1.10</option>
  </select>
  <button onclick="createSession">Submit</button>
</div>

You need to set the handlers to call your function, not just assign:您需要设置处理程序来调用您的函数,而不仅仅是分配:

<button onclick="createSession()">Submit</button>

Also, you can just create the url in the createSession function:此外,您可以在createSession函数中创建 url:

 function createSession() { var ip = document.getElementById("apigatewayip").value; var _url = "https://" + ip + "/api/v0/sessions" console.log(_url) // Use this for ajax call /*$.ajax({ "url": _url, "method": "POST", "timeout": 0, "headers": { "Content-Type": "application/json" }, "data": JSON.stringify({ "gatewayVersion": "1.00.1.15" }), });*/ }
 <h1>Making AJAX Rest Calls</h1> <div class="custom-select" style="width:200px;"> <select name="selectip" id="apigatewayip"> <option value="" disabled selected>API Gateway IP</option> <option value="169.254.1.10">169.254.1.10</option> </select> <button onclick="createSession()">Submit</button> </div>

Wait for the document to render using document ready $(function() { }) and use jQuery, don't mix with legacy JavaScript selection使用文档就绪$(function() { })等待文档呈现并使用 jQuery,不要与遗留的 JavaScript 选择混合

Use done callback instead of success使用done回调代替success

//Global function 1
function makeUrl() {
    var ip = $("#apigatewayip").val();
    return "https://" + ip + "/api/v0/sessions"
};
//Global function 2
function createSession(_url) {
  return $.ajax({
    "url": _url,
    "method": "POST",
    "timeout": 0,
    "headers": {
      "Content-Type": "application/json"
    },
    "data": JSON.stringify({
      "gatewayVersion": "1.00.1.15"
    }),
  });
}


//Wait until document fully render
$(function() {
   //After document render call the methods
   createSession(makeUrl());

   createSession(makeUrl()).done(function() {

   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM