如果错误,请重新翻转复选框



我有一个复选框,上面有一些风格,所以它看起来像是在翻转。当用户按下按钮时,它会在客户端直观地翻转,但我向服务器发送请求以将该更改保存到数据库中。如果发生一些错误,我想在客户端重新翻转它。我会知道是否发生了什么,因为会引发 http.post(( 的错误回调。我的问题是如何将该复选框翻转回其原始状态?

这是我用于复选框的视觉对象。我正在使用可翻转的: https://codepen.io/anon/pen/yXNopJ?editors=1100

下面是复选框的示例(每个月一个(

<input class="tgl tgl-flip" id="cbJan_{{ s.MFGName }}" type="checkbox" ng-model="s.Jan" ng-change="vm.monthValueChanged(s.MFGName, 'Jan', s.Jan)" />
<label class="tgl-btn" data-tg-off="OFF" data-tg-on="ON" for="cbJan_{{ s.MFGName }}" ></label>

在我的javascript代码中,我尝试使用jquery来构建id并检查它,但这没有任何作用。它没有错误,也没有还原复选框。

function (error, status) {
alertify.delay(0).maxLogItems(3).closeLogOnClick(true).error('Error: ' + error.data.Message + ' <a href="#" class="close-icon"></a>');
var data = JSON.parse(error.config.data);
var name = 'cb' + data.Month + '_' + data.MFG;
console.log(name);
$(name).prop('checked', !data.Value);
// todo: negate data.Value to get the original value and set the checkbox to that state programmatically
}

谢谢你们和我一起提出想法。我能够通过传入模型作为最后一个参数来使其工作:

ng-change="vm.monthValueChanged(s.MFGName, 'Jan', s.Jan, s)"

这让我获得了正确的 MFG 记录,但随后我需要知道哪个月列。幸运的是,我已经在"一月"过了。所以我可以在数组中获取正确的数据片段并翻转它,从而导致复选框的视觉翻转。

model[month] = !data.Value;

最新更新