Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选中单选按钮时,在输入字段中添加必需的属性_Javascript_Html_Web - Fatal编程技术网

Javascript 选中单选按钮时,在输入字段中添加必需的属性

Javascript 选中单选按钮时,在输入字段中添加必需的属性,javascript,html,web,Javascript,Html,Web,我有一个id为=“radio jadwal”的单选按钮。我想让我的2个输入字段(日期和时间)在单击按钮时成为必需的。这个问题的最佳解决方案是什么。提前感谢 <form> <fieldset id="group1"> <input type="radio" value=""> <input type="radio" value="">

我有一个id为=“radio jadwal”的单选按钮。我想让我的2个输入字段(日期和时间)在单击按钮时成为必需的。这个问题的最佳解决方案是什么。提前感谢


<form>
<fieldset id="group1">
    <input type="radio" value="">
    <input type="radio" value="">
</fieldset>

<fieldset id="group2">
    <input type="radio" value="">
    <input type="radio" value="">
    <input type="radio" value="">
</fieldset>

您必须执行两个步骤。 单击单选按钮调用一个方法。 之后,确保它被标记为check,并将所需属性添加到输入中

功能clickHandler(收音机){
document.getElementById(“名称”).required=radio.checked;
document.getElementById(“日期”).required=radio.checked;
}

您可以这样做:

let input=document.querySelector(“#radio jadwal”);
让inputRequired=document.querySelectorAll('input[class=“input required”]”);
input.addEventListener('click',function()
{
如果(选中此项)
{
inputRequired.forEach((索引,i)=>
{
index.setAttribute('required',true);
控制台日志(索引);
});
}
其他的
{
inputRequired.forEach((索引,i)=>
{
index.setAttribute('required',false);
控制台日志(索引);
});
}
});

  • 如果您使用的是香草JavaScript-
  • 定义一个布尔值(例如x),它将根据是否选中单选按钮(1)或(0)来更改其值。如果x为1,则使用“setAttribute()”将“必需”功能添加到元素中。如果x为0,则使用“removeAttribute()”删除“必需”功能

  • 如果您使用的是React之类的东西,那么可以使用状态变量来跟踪按钮的“选中”或“未选中”状态
  • 已选中
    $(“#radio jadwal”)。单击(函数(){
    $(“.cm”).prop('required',true);
    });
    
    如果else是最好的解决方案I thing
    if(radio.checked)
    您甚至不需要它,只需直接分配值,因为它是布尔值,它将是正确或错误的好点。更改了我的答案@abhishekpandeyal所以它应该是更改的,而不是单击,这样它可以在分组无线电中工作
    if(radio.checked)
    您甚至不需要它,只需直接分配值,因为它是布尔值,它可能是真的,也可能是假的,您可以通过
    'input[class=“input required”]“
    为什么不
    ”输入。需要输入“
    ”。需要输入“
    ”?这只是一种识别特定元素的方法。有很多方法可以做到这一点;)@请给你的答案添加一些解释,以便其他人可以从中学习
    <input type='radio' id='radio-jadwal'>Checked
    <input type='text' class='cm'>
    <input type='text' class='cm'>
    
    <script>
        $('#radio-jadwal').click(function() {
            $(".cm").prop('required',true);
        });
    </script>