这里是新手,试图向队友展示客户端休息操作的值(value),因为他们依赖脚本和Python。不确定是否是我使用数字作为 value 属性,或者是否引用了 getelementbyid 错误。老实说,可能不是这些事情:)
我尝试在 MakeUrl 的 getelementbyid 中显式引用分配给 IP 地址的 id,但仍然失败。我还以为它是空的,因为它使用的是默认值,但我将其注释掉,但仍然收到错误。
错误:
Uncaught TypeError: Cannot read property 'value' of null (MakUrl Function)
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>
最佳答案
您需要设置处理程序来调用您的函数,而不仅仅是分配:
<button onclick="createSession()">Submit</button>
此外,您也可以在 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>
关于javascript - HTML 选择下拉菜单 ID 未传递给 Javascript 函数 getelementbyid 值为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60970871/