【问题标题】:Electron: Check box not changing value?电子:复选框不改变值?
【发布时间】:2021-03-11 05:40:26
【问题描述】:

我创建了一个浏览器窗口,它使用复选框来打开和关闭自身,但是,只有处于 true 状态的复选框被检测到,而不是处于 false 状态。

这是我在 renderer.js 中的代码:

const {width, height} = screen.getPrimaryDisplay().workAreaSize;
let close = document.querySelector('input[name=open_close]');
close.addEventListener('change', function(event){
  if (this.checked = true) {
  console.log("renderer");
 ipcRenderer.send('expClose',width, height);
  }

  if(this.checked = false) {
 ipcRenderer.send('expOpen',width - 500, height - 450);
  }
});

这是我的 main.js:

ipcMain.on('expClose', function(e,width, height) {
    console.log('closed'); 
      mainWindow.setPosition(width - 75, height - 450);
  });
  
  ipcMain.on('expOpen', function(e,width, height) {
   console.log("opened")
   mainWindow.setPosition(width, height);
      
  });

如果需要,还有我的 HTML:

<input type="checkbox" name="setbtn" checked="false" id="settings" class="btnset">

我假设未检测到错误状态,因为未记录打开状态。 任何帮助将不胜感激。

【问题讨论】:

  • 你的复选框应该命名为“open_close”,不是吗?
  • @SydneyY 我嗯...粘贴了错误的 htm,我把它们太相似了

标签: javascript html electron


【解决方案1】:

您使用 = (assignment) 而不是 === (比较)

if (this.checked = true) { 更改为if (this.checked === true) {
if(this.checked = false) {if(this.checked === false) {

【讨论】:

    【解决方案2】:

    我设法让它与这段代码一起工作:

    const {width, height} = screen.getPrimaryDisplay().workAreaSize;
    let close = document.querySelector('input[name=open_close]');
    close.addEventListener('change', function(event){
      console.log("renderer");
     ipcRenderer.send('expClose',width, height);
    
      if(document.querySelector('input[name=open_close]:checked') !==null ) {
     ipcRenderer.send('expOpen',width - 500, height - 450);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多