如何用常规的JavaScript函数/约定替换$(document).ready?

huangapple go评论67阅读模式
英文:

how to replace $(document).ready with normal Javascript functions/conventions?

问题

所以我有以下的代码

$(document).ready(function(){
    // 用户选择单选按钮后启用提交按钮
    if (document.querySelector('input[name="benefitType"]')) {
        document.querySelectorAll('input[name="benefitType"]').forEach((elem, i) => {
            elem.addEventListener("change", function(event) {
                document.querySelector('#benefit-quote-submit').disabled = false;
            });
        });
    }

基本上,我在表单中选择单选按钮时启用提交按钮。

我想用普通的Javascript替换$(document).ready

我尝试过

if(document.readyState === 'ready' || document.readyState === 'complete') {
  doSomething();

但它不起作用(当我说它不起作用时,我的断点在此行"elem.addEventListener"上不会中断执行。它在$(document).ready中正常工作。

谢谢。

英文:

so I have the following code

    $(document).ready(function(){
        //enable submit button once user makes a radio button selection
        if (document.querySelector('input[name="benefitType"]')) {
            document.querySelectorAll('input[name="benefitType"]').forEach((elem, i) => {
                elem.addEventListener("change", function(event) {
                    document.querySelector('#benefit-quote-submit').disabled = false;
                });
            });
        }

basically, I am enabling the submit button (of type <button) when one of the radio buttons in the form is selected.

I want to replace $(document).ready with normal Javascript equivalents.

I tried

if(document.readyState === &#39;ready&#39; || document.readyState === &#39;complete&#39;) {
  doSomething();

but it's not working (when I said it's not working, my breakpoint on this line "elem.addEventListener" is not breaking the execution. It works fine though with $(document).ready.

Thank you.

答案1

得分: 2

当网站加载完成时,您想执行某些操作。

您可以在body元素上使用onload事件:

document.body.onload = () => doSomething();

或者您也可以通过添加事件监听器来实现相同的效果:

document.body.addEventListener("DOMContentLoaded", () => doSomething());

希望这对您有所帮助!

英文:

So you want to do some action when the website is loaded.

You can use the onload event on the body:

document.body.onload = () =&gt; doSomething();

Or you can achieve the same by adding an event listener:

document.body.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; doSomething());

I hope this helps!

答案2

得分: 0

文档/DOM 在文档上发生 DOMContentLoaded 事件时准备就绪。

所以下面是一个完整的示例,说明当表单有效时如何启用按钮。无需在每个输入上添加事件侦听器。当表单中发生某些事件时,只需使用 checkValidity() 来测试表单即可。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
document.addEventListener('DOMContentLoaded', e => {
  // 表单提交的事件监听器
  document.forms.form01.addEventListener('submit', e => {
    e.preventDefault();
    console.log('You selected:', e.target.benefitType.value);
  });
  // 当表单中发生更改时
  // 测试是否有效并更改按钮
  document.forms.form01.addEventListener('input', e => {
    if (e.target.form.checkValidity()) {
      e.target.form.sumitbtn.disabled = false;
    }
  });
  document.forms.form01.addEventListener('change', e => {
    if (e.target.form.checkValidity()) {
      e.target.form.sumitbtn.disabled = false;
    }
  });
});

<!-- 语言: lang-html -->
<form name="form01">
  <label>X<input type="radio" name="benefitType" value="x" required></label>
  <label>Y<input type="radio" name="benefitType" value="y"></label>
  <label>Z<input type="radio" name="benefitType" value="z"></label>
  <button name="sumitbtn" disabled>Submit</button>
</form>

<!-- 结束代码片段 -->
英文:

The document/DOM is ready when the DOMContentLoded event happens on the document.

So here is a complete example of how the button can be enabled when the form is valid. There is no reason to have event listeners on each input. When something happens in the form you can just test using checkValidity() on the form.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener(&#39;DOMContentLoaded&#39;, e =&gt; {
  // event listener for submitting the form
  document.forms.form01.addEventListener(&#39;submit&#39;, e =&gt; {
    e.preventDefault();
    console.log(&#39;You selected:&#39;, e.target.benefitType.value);
  });
  // When something has changed in the form
  // test if valid and change button
  document.forms.form01.addEventListener(&#39;input&#39;, e =&gt; {
    if(e.target.form.checkValidity()){
      e.target.form.sumitbtn.disabled = false;
    }
  });
  document.forms.form01.addEventListener(&#39;change&#39;, e =&gt; {
    if(e.target.form.checkValidity()){
      e.target.form.sumitbtn.disabled = false;
    }
  });
});

<!-- language: lang-html -->

&lt;form name=&quot;form01&quot;&gt;
  &lt;label&gt;X&lt;input type=&quot;radio&quot; name=&quot;benefitType&quot; value=&quot;x&quot; required&gt;&lt;/label&gt;
  &lt;label&gt;Y&lt;input type=&quot;radio&quot; name=&quot;benefitType&quot; value=&quot;y&quot;&gt;&lt;/label&gt;
  &lt;label&gt;Z&lt;input type=&quot;radio&quot; name=&quot;benefitType&quot; value=&quot;z&quot;&gt;&lt;/label&gt;
  &lt;button name=&quot;sumitbtn&quot; disabled&gt;Submit&lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 17:26:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76321998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定