简体   繁体   English

淘汰js订阅以选择列表selected选项反复显示未定义

[英]knockout js subscribing to select list selected option is showing undefined repeatedly

I have I drop down list prepopulated with a list of strings. 我下拉列表中预填充了字符串列表。 My select list binding is like so: 我的选择列表绑定是这样的:

<select id="industryDropDown"
    data-bind="options: $root.orgIndustrySuggestions, value: $root.selectedIndustryTag, optionsCaption: ''"></select>

In my viewmodel I have this: 在我的视图模型中,我有:

self.selectedIndustryTag.subscribe(function (newValue) {
    if (newValue !== '') {
        if (newValue !== 'undefined')
            {
        alert(self.selectedIndustryTag());
        self.selectedIndustryTag('');
        }
    }
}, self);

I am trying to get the selected value from the drop down list, do something where the alert() is now, and then reset the selected value to the default empty string option. 我正在尝试从下拉列表中获取选定的值,现在执行alert()的操作,然后将选定的值重置为默认的空字符串选项。

Currently when I run this, page load is fine. 目前,当我运行此命令时,页面加载就可以了。 But when I select something it will show me the selected value the first time, but them throw up a kajillion (i counted) alerts saying 'undefined' after that. 但是,当我选择某项内容时,它将第一次向我显示选定的值,但是它们会抛出一千亿(我算过)警报,然后说“未定义”。

You are over complicating your logic for one. 您过于复杂了一个逻辑。 Create a fiddle demonstrating your error and we could probably show you why, but in a nutshell you have a recursive loop (you are subscribing to a value and setting it inside of that function) and you are also testing multiple times when you don't need to - 创建一个小提琴来演示您的错误,我们可能会向您展示原因,但是总而言之,您有一个递归循环(您正在订阅一个值并将其设置在该函数内部),并且在不进行该操作的情况下还要进行多次测试需要 -

self.selectedIndustryTag.subscribe(function (newValue) {
    if (newValue) {
        alert(self.selectedIndustryTag());
         // The next line will refire the subscription
         //self.selectedIndustryTag('');
    }
}, self);

The conditional statement - if (newValue) will test whether newValue equals null, undefined, false, or string empty ('') 条件语句-if(newValue)将测试newValue是否等于null,undefined,false或字符串empty('')

Was that exactly a kajillion? 整整 kajillion? or was it more like ten bajillion? 还是更像十兆?

The reason you have this problem is that you're entering an endless loop. 您遇到此问题的原因是您正在进入一个无休止的循环。 A change to selectedIndustryTag in the UI raises the subscribe. UI中对selectedIndustryTag的更改将引发订阅。 Inside the subscribe you change selectedIndustryTag which raises the subscribe which changes the selectedIndustryTag which.... well you probably get the point. 在订阅中,您更改了selectedIndustryTag ,这引发了订阅,而订阅又更改了selectedIndustryTag ,....

The way to resolve this is to have 2 properties. 解决此问题的方法是具有2个属性。 The live one edited by the UI, which you can subscribe to, and a second one which you use internally (which the subscribe can update). 您可以订阅由用户界面编辑的实时直播,您可以在内部使用的直播直播(订阅可以更新)。

Another issue you have, you're using strict non-equality operator !== but comparing to the string 'undefined' . 您遇到的另一个问题是,您正在使用严格的非等号运算符!==但与字符串'undefined'进行比较。 That will never evaluate as you should be comparing against undefined (without quotes). 这将永远不会评估,因为您应该将其与undefined (不带引号)进行比较。

so change 所以改变

if (newValue !== 'undefined')

to

if (newValue !== undefined)

Live example: http://jsfiddle.net/2NqFY/ 实时示例: http//jsfiddle.net/2NqFY/

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

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