0

好的,所以我对 javascript 还很陌生,我正在为我的实习开发一个 web 应用程序的前端,这完全让我感到困惑。

我有一个 select 和一个 input 元素,它们都通过 .POST() 输入到数据库过程调用的消息模型中。我已经在项目的其他脚本中完成了此操作,但是无论我如何尝试分配值,它都无法正常工作。

这是代码:

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);

输出:

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

我尝试了多种方法:

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

这些都不起作用。为什么不赋值?

4

1 回答 1

0

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.

The JS interpreter when you are trying to access a non existing attributes do not throw an error, instead he return an undefined value.

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 !

于 2018-02-14T16:28:43.280 回答