简体   繁体   English

使用 javascript 单击 li 时检查单选按钮

[英]check the radio button when click on the li using javascript

I have radio button我有单选按钮

Html code: Html 代码:

<input type="radio" class="first" name="bright" checked>
<input type="radio" class="second" name="bright" >
<input type="radio" class="third" name="bright">
<input type="radio" class="four" name="bright">

And i have a nav bar我有一个导航栏

Html code Html代码

<ul class="nav">

<li class="st st1 active" data-cont="first">
<h2 class="inner">وزارة الاستثمار</h2>
</li>
<li class="st st2" data-cont="second">
<h2 class="inner">وزارة التجارة</h2>
</li>
<li class="st st3" data-cont="third">
<h2 class="inner">جهات حكومية اخرى</h2>
</li>
<li class="st st4" data-cont="four">
<h2 class="inner">مكتب هندسي</h2>
</li>
 </ul>

These 2 are conected with the data-cont that have the class of the radio button这 2 个与具有单选按钮的 class 的 data-cont 连接

I want when i click on the li the correct radio button be checked using javascript我希望当我单击 li 时使用javascript检查正确的单选按钮

I tried to make it using this code in JavaScript我尝试在JavaScript中使用此代码

let radio = document.querySelectorAll("input");
let radioArray = Array.from(radio);
let tabs = document.querySelectorAll(".nav li");
let tabsArray = Array.from(tabs);


tabsArray.forEach((ele) => {
    ele.addEventListener("click", function (e) {
        tabsArray.forEach((ele) => {
            ele.classList.remove("active");
        });
        e.currentTarget.classList.add("active");
       document.querySelector(e.currentTarget.dataset.cont).checked = true;
    });
});

I try to remove the active class from li and put it on the li where i click then i want the radio button be checked我尝试从 li 中删除活动的 class 并将其放在我单击的 li 上,然后我希望选中单选按钮

Any body can help me on this?任何机构都可以帮助我吗?

the last querySelector is where your code is failing you're not referencing the class for your input it needs to be document.querySelector('.' + e.currentTarget.dataset.cont).checked = true;最后一个 querySelector 是您的代码失败的地方,您没有引用 class 作为输入,它需要是document.querySelector('.' + e.currentTarget.dataset.cont).checked = true; note the "."注意“。” prefix字首

Although that answers your question there is probably more value in pointing out that by changing your html markup to be a little more accessible you can eliminate the need for all of the javascript in your example尽管这回答了您的问题,但指出通过将 html 标记更改为更易于访问,您可以消除对示例中所有 javascript 的需求。

eg例如

 input:checked + label { color:Red; }
 <div><input type="radio" id="first" name="bright" checked> <label for='first'>وزارة الاستثما</label> </div> <div> <input type="radio" id="second" name="bright" > <label for='second'>وزارة التجارة</label> </div> <div> <input type="radio" id="third" name="bright"> <label for='third'>جهات حكومية اخرى</label> </div> <div> <input type="radio" id="four" name="bright"> <label for='four'>مكتب هندسي</label> </div>

The use of labels associated with your radio buttons is now significantly more accessible and you can drastically reduce a lot of your markup ( though to be accessible you would need to provide a more meaningful name for for attribute.与单选按钮关联的标签的使用现在明显更易于访问,并且您可以大大减少大量标记(尽管要易于访问,您需要for属性提供更有意义的名称。

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

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