2015-11-03 790 views
1

这个问题似乎基本,但是不要紧我做什么(基于SOV的各种帖子中,我经历了)我只是不能得到这个事件在我Devextreme应用火JQuery的onBlur事件不触发

的前提... 我想要做的就是用输入的文本更新现有变量的值。变量的值从另一个页面继承。我需要从写入文本框的文本更新变量的值。这就是我想要做的。

这里是我的html代码:

<div class="dx-field-label">Code: </div> 
    <div class="dx-field-value" id="txtCode" data-bind="dxTextBox: { value: CustomerCode, placeholder: 'Enter Code', showClearButton: true }"></div> 
</div> 

这里是我的JS代码:

var viewModel = { 
    // CustomerID: params.CustomerID, 
    // CustomerName: ko.observable(params.CustomerName), 
    //CustomerEmail: ko.observable(params.CustomerEmail), 
    // CustomerID: ko.observable(params.CustomerID), 

    Continue: function() { 
     alert('Saved'); 
    }, 
    CustomerID: CustomerID, 
    CustomerName: CustomerName, 
    CustomerCode: CustomerCode, 
    CustomerEmail: CustomerEmail, 
    AddCustomer: AddCustomer, 

    //Add Customer 
}; 

$('#txtCode').on('blur', 'txtCode', function() { 
    CustomerCode = $("#txtCode").dxTextBox('option', 'value');  
    console.log(CustomerCode); 
}); 

任何意见,将不胜感激

编辑

OK,我现在已经做了这个:

CustomerID; 
CustomerName.trim(); 
CustomerCode.trim(); 
CustomerEmail.trim(); 



ko.applyBindings({ 
ChangedCustomerCode: ko.observable(), 
CodeFocusOut: function(e){ 
    ChangedCustomerCode: value; 
    console.log(value); 
    console.log(ChangedCustomerCode); 
    CustomerCode: ChangedCustomerCode; 
    console.log(CustomerCode); 
} 

});

var viewModel = { 

    Continue: function() { 
     alert('Saved'); 

    }, 



    CustomerID: CustomerID, 
    CustomerName: CustomerName, 
    CustomerCode: CustomerCode, 
    CustomerEmail: CustomerEmail, 
    AddCustomer: AddCustomer, 


}; 


return viewModel; 
}; 

但它告诉我,我不能将绑定多次应用到相同的elemenet。我该怎么办?

+0

当绑定事件,''txtCode'',缺少'.'?或者只是'$('#txtCode')。on('blur',function(){' – Tushar

+0

也请确保在'#txtCode'内输入'.txtCode'类 – Mitul

回答

0

使用事件,像这样绑定:

$('.txtCode').on('blur', function ($this) { 
    // Code. 
} 
0

试试这个

$('body').on('blur', '#txtCode', function() { 
    CustomerCode = $("#txtCode").dxTextBox('option', 'value'); 

console.log(CustomerCode); 
}); 
1

您尝试处理 '的div' 元素(id="txtCode")在blur事件。但此元素的焦点状态不会更改,因此blur事件不会触发。

其实,如果你使用的是knockoutjs,你不应该手动处理blur事件。只需使用ko.observables:

<div data-bind="dxTextBox: { value: myValue, onValueChanged: valueChanged }"></div> 

ko.applyBindings({ 
    myValue: ko.observable(), 
    valueChanged: function(e){ 
     alert(e.value); 
    } 
}); 

看到这个小提琴 - http://jsfiddle.net/0284uz1f/2/

+0

谢谢您的回复谢尔盖,但现在我得到错误说明我不能将绑定多次应用于同一个元素我使用新代码更新了我的原始文章 –

+0

如果使用Devextreme应用程序项目模板,则不应该手动调用ko.applyBindings,因为我之所以称它为我只用了knockoutjs。 – Sergey