简体   繁体   English

Console.log() 它没有显示函数的结果

[英]Console.log() it's not showing the result of a function

I'm trying to get the value from a lightning:select but when I try a console.log in the js controller of the component, it doesn't show anything.我正在尝试从 Lightning:select 中获取值,但是当我在组件的 js 控制器中尝试使用 console.log 时,它没有显示任何内容。

I put the same function exactly in 'Inspect Element > Console' and it works perfectly.我将相同的功能完全放在“检查元素> 控制台”中,它运行良好。

Component:成分:

<lightning:select name="{!'mySelectedStatus' + case.CaseNumber}">
                                            <option text="All" value="" selected="true"/>
                                            <aura:iteration items="{!v.caseStatuses}" var="status">
                                                <option text="{!status}" value="{!status}" />
                                            </aura:iteration> 
                                        </lightning:select>

Controller.js:控制器.js:

changeStatus : function(component, event, helper) {
        var csNum = event.getSource().get("v.name");
        console.log(document.getElementsByName("mySelectedStatus00001044")[0].value); //Fails Here
        var status = document.getElementsByName("mySelectedStatus"+csNum)[0].value;

        helper.changeStatus(component, csNum, status);
    }

In the Inspect Element Console I get the result: document.getElementsByName("mySelectedStatus00001044")[0].value);在 Inspect Element Console 我得到结果: document.getElementsByName("mySelectedStatus00001044")[0].value); Result => "New"结果 =>“新”

But in controller.js console.log() I get blank.但是在 controller.js console.log() 我得到空白。 Why?为什么?

Try something like this:尝试这样的事情:

  1. Add attribue "label" on lightning:select as it is mandatory.在 Lightning:select 上添加属性“标签”,因为它是强制性的。

  2. Assign aura:id分配光环:id

  3. Define the onchange event to capture the latest changed value定义 onchange 事件以捕获最新更改的值

    <aura:component>
        <aura:attribute name="test" type="String[]" default="['A','B']" />
        <lightning:select name="mySelectedStatus" label="selectlist" onchange=" {!c.changeStatus}" aura:id="mySelectedStatus">
            <option text="All" value="" selected="true"/>
            <aura:iteration items="{!v.test}" var="status">
                <option text="{!status}" value="{!status}" />
            </aura:iteration>
        </lightning:select>
    </aura:component>

JS Controller: JS控制器:

    changeStatus : function(component, event, helper) {
        console.log(component.find("mySelectedStatus").get("v.value"));
    }

Hope this helps!!!希望这可以帮助!!!

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

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