简体   繁体   English

javascript变量的HTTP输入仍然未定义

[英]HTTP input to javascript variable remains undefined

Okay, so I'm fairly new to javascript and I'm working on the front-end of a webapplication for my internship, and this just utterly baffles me. 好的,所以我对javascript还是很陌生,我正在为自己的实习工作在Web应用程序的前端,这完全让我感到困惑。

I have a select and an input element, both of which feed into a message model for a database Procedure Call through .POST(). 我有一个select和一个input元素,这两个元素都输入到数据库的消息模型中通过.POST()进行调用。 I've done this in other scripts in the project, but here it won't work no matter how I try to assign the values. 我已经在项目中的其他脚本中完成了此操作,但是无论我如何尝试分配值,在这里它都不起作用。

Here's the code: 这是代码:

var cctOldSelect = document.getElementById("ConceptToCopy");
var cctOld = cctOldSelect.options[cctOldSelect.selectedIndex].value;
var cctNew = document.getElementById('NewConceptName').value;
console.log("cctOld: " + cctOld + "; cctNew: " + cctNew);
if (cctOld !== "" && cctNew !== "") {
    console.log("model creation started.");
    var model = {
        p_cct_code_old: cctOldSelect.options[cctOldSelect.selectedIndex].value,
        p_cct_code_new: document.getElementById('NewConceptName').value,
        p_specialty_layout: null
    };
    console.log("model creation ended.");
}
console.log("cctOld model: " + model.cctOld + "; cctNew model: " + model.cctNew);

output: 输出:

cctOld: 1020H; cctNew: 1021H
model creation started.
model creation ended.
cctOld model: undefined; cctNew model: undefined

I've tried multiple ways: 我尝试了多种方法:

p_cct_code_Old: $scope.<ElementID>  //both with and without .value
p_cct_code_Old: document.getElementById(<ElementID>)    
var cctOld = document.getElementById(<ElementID>); p_cctOld: cctOld

None of which work. 都不起作用。 Why won't it assign a value? 为什么不分配值?

The problem in this case is that you create a new object called "model" and with two attributes called "p_cct_code_old" and "p_cct_code_new", then you are trying to access two attributes called "cctOld" and "cctNew" out of scope . 这种情况下的问题是,您创建了一个名为“模型”的新对象,并具有两个名为“ p_cct_code_old”和“ p_cct_code_new”的属性,然后您试图访问两个超出范围的属性“ cctOld”和“ cctNew”。

The JS interpreter when you are trying to access a non existing attributes do not throw an error, instead he return an undefined value. 当您尝试访问不存在的属性时,JS解释器不会引发错误,而是会返回未定义的值。

The thing that I suggest to make the things work is: 我建议使这些东西起作用的是:

var model = {};
var cctOldSelect = document.getElementById("ConceptToCopy");
var cctOld = cctOldSelect.options[cctOldSelect.selectedIndex].value;
var cctNew = document.getElementById('NewConceptName').value;
console.log("cctOld: " + cctOld + "; cctNew: " + cctNew);
if (cctOld !== "" && cctNew !== "") {
    console.log("model creation started.");
    model.p_cct_code_old = cctOldSelect.options[cctOldSelect.selectedIndex].value;
    model.p_cct_code_new = document.getElementById('NewConceptName').value;
    model.p_specialty_layout: null;
    console.log("model creation ended.");
}
console.log("cctOld model: " + model.p_cct_code_old + "; cctNew model: " + model.p_cct_code_new);

Tell me if this solution helped you ! 告诉我该解决方案是否对您有帮助!

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

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