简体   繁体   English

使js触发不止一次?

[英]Make js fire more than once?

I have a set of radio buttons, and I want this code to fire every time I click the radio buttons, but it only displays the div with its content once, and then it's no longer working. 我有一组单选按钮,我希望每次单击单选按钮时都触发此代码,但它只将div及其内容显示一次,然后不再起作用。 I'm not sure what I'm doing wrong here. 我不确定我在做什么错。

Try setting the #adr div's innerHTML to nothing (' ') rather than its display to "nothing". 尝试将#adr div的innerHTML设置为“无”(而不是“无”)。
Change it to: 更改为:

else if (document.getElementById('nothing').checked) {
    document.getElementById('adr').style.innerHTML = ''; // <-- here
}

Instead of: 代替:

else if (document.getElementById('nothing').checked) {
    document.getElementById('adr').style.display = 'none'; // <-- here
}

So instead of hiding the div (so when you next change its innerHTML nothing displays) you change the innerHTML to nothing, so that when you change it back it displays the content 因此,与其隐藏div(因此,当您下次更改其innerHTML不显示任何内容),而是将innerHTML更改为innerHTML ,以便当您将其更改回时,它显示内容

This is because you set the display of the div to none in the block that is run if the nothing radio button is checked, and you don't set the display to block when it is set to the option that shows the div, so the display remains as none. 这是因为,如果选中“ none”单选按钮,则在运行的块中将div的显示设置为“ none”,并且在将其设置为显示div的选项时,也不会将显示设置为“ block”。显示保持为空。 My solution is this: Keep the contents of the street div the same, and only toggle its display value. 我的解决方案是:保持街道div的内容相同,仅切换其显示值。 Even better, toggle visibility instead, unless you MUST set display to none. 更好的是,改为切换可见性,除非您必须将显示设置为无。

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

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