英文:
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 === 'ready' || document.readyState === 'complete') {
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 = () => doSomething();
Or you can achieve the same by adding an event listener:
document.body.addEventListener("DOMContentLoaded", () => 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('DOMContentLoaded', e => {
// event listener for submitting the form
document.forms.form01.addEventListener('submit', e => {
e.preventDefault();
console.log('You selected:', e.target.benefitType.value);
});
// When something has changed in the form
// test if valid and change button
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;
}
});
});
<!-- language: 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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论