简体   繁体   English

根据 html 将 jQuery 代码转换为 javascript

[英]Convert jQuery code into javascript depending html

I have html and js code.我有 html 和 js 代码。 But the javascript code is by jQuery and I want to convert this jQuery code to Javascript:但是 javascript 代码是 jQuery ,我想将此 jQuery 代码转换为 Z9E13B69D1D2DA927102ACAAAF7154A3

// jquery code
$('.hello[type="checkbox"]').on('change', function() {
   $(this).siblings('.hello[type="checkbox"]').not(this).prop('checked', false);
});

and my html markup:和我的 html 标记:

<div>
    <span>Group 1:</span>
    <input type="checkbox" class="group1 hello" />
    <input type="checkbox" class="group1 hello" />
    <input type="checkbox" class="group1 hello" />
</div>

<div>
    <span>Group 2:</span>
    <input type="checkbox" class="group2 hello" />
    <input type="checkbox" class="group2 hello" />
    <input type="checkbox" class="group2 hello" />
</div>

<div>
    <span>Group 3:</span>
    <input type="checkbox" class="group3 hello" />
    <input type="checkbox" class="group3 hello" />
    <input type="checkbox" class="group3 hello" />
</div>

i want to use this code for check box input that i want to checked only one input by user我想将此代码用于复选框输入,我只想检查用户的一个输入

view demo查看演示

i try this code by changing and add attribute in html but it don`t work properly for group in the my questin我通过在 html 中更改和添加属性来尝试此代码,但它不适用于我的任务中的组

function checkOnlyOne(b){

var x = document.getElementsByClassName('daychecks');
var i;

for (i = 0; i < x.length; i++) {
  if(x[i].value != b) x[i].checked = false;
}
}

<div>
    <span>Group 3:</span>
    <input type="checkbox" class="group3 hello" onclick="checkOnlyOne(this.value); />
    <input type="checkbox" class="group3 hello" onclick="checkOnlyOne(this.value); />
    <input type="checkbox" class="group3 hello" onclick="checkOnlyOne(this.value); />
</div>

You can use querySelectorAll method to get elements using CSS selector.您可以使用querySelectorAll方法使用 CSS 选择器获取元素。

 // jquery code document.querySelectorAll('.hello[type="checkbox"]').forEach(el => { el.addEventListener('change', function() { this.parentNode.querySelectorAll('.hello[type="checkbox"]').forEach(el1 => { if (el.== el1) el1;checked = false; }); }); });
 <div> <span>Group 1:</span> <input type="checkbox" class="group1 hello" /> <input type="checkbox" class="group1 hello" /> <input type="checkbox" class="group1 hello" /> </div> <div> <span>Group 2:</span> <input type="checkbox" class="group2 hello" /> <input type="checkbox" class="group2 hello" /> <input type="checkbox" class="group2 hello" /> </div> <div> <span>Group 3:</span> <input type="checkbox" class="group3 hello" /> <input type="checkbox" class="group3 hello" /> <input type="checkbox" class="group3 hello" /> </div>


FYI: You can achieve the same behaviour with radio button without any additional Javascript code, Just need to provide same name attribute for a group.仅供参考:您可以使用单选按钮实现相同的行为,无需任何额外的 Javascript 代码,只需为组提供相同的名称属性。

 <div> <span>Group 1:</span> <input type="radio" name="group1" class="group1 hello" /> <input type="radio" name="group1" class="group1 hello" /> <input type="radio" name="group1" class="group1 hello" /> </div>

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

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