Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.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_Checkbox_Custom Fields_Required - Fatal编程技术网

Javascript 单击复选框后,表单字段是否变为必填字段?

Javascript 单击复选框后,表单字段是否变为必填字段?,javascript,html,checkbox,custom-fields,required,Javascript,Html,Checkbox,Custom Fields,Required,我正在使用一个复选框,单击该复选框时,它会显示/隐藏字段。我希望仅当我单击复选框并显示字段时,我的字段才需要输入信息。如果字段被隐藏,并且未单击复选框,则不需要这些字段来输入信息。我该如何做到这一点。这是我正在使用的代码 <script type="text/javascript" language="JavaScript"> function HidePart(d) { document.getElementById(d).style.display = "none"; } fu

我正在使用一个复选框,单击该复选框时,它会显示/隐藏字段。我希望仅当我单击复选框并显示字段时,我的字段才需要输入信息。如果字段被隐藏,并且未单击复选框,则不需要这些字段来输入信息。我该如何做到这一点。这是我正在使用的代码

<script type="text/javascript" language="JavaScript">
function HidePart(d) { 
document.getElementById(d).style.display = "none";
}
function ShowPart(d) { 
document.getElementById(d).style.display = "block"; 
}
function CheckboxChecked(b,d)
{
if(b) {
ShowPart(d); 
}
else  { 
HidePart(d); 
}
}
</script>


  <input type="checkbox" name="Loan2" value="yes" onclick="CheckboxChecked(this.checked,'checkboxdiv2')"> Add New Loan


<div id="checkboxdiv2" style="display:none">
Form Fields for input information
</div> 

如果这有帮助,这是我的网站:。当我点击AddNewLoan时,它会生成更多的字段,我只想在这些字段显示后才需要这些字段。我希望我的问题是清楚的,我希望得到一些帮助来调整这个javascript代码,使其按照我需要的方式运行。任何帮助都将不胜感激

将表单验证代码放入额外的if语句中。试着这样做:

if (document.loan2.checked) {
  // validate the input
} else {
  return false;
}
我尽量让你明白这一点。只要看看评论,一切都会好起来的


如果你有任何问题,请告诉我。希望这对您有所帮助

ur表单在单击时不会提交-您的导入脚本有问题-应提交部分
<html>
<head>
  <!--Add this JQuery library to make this a whole lot easier for you-->
  <!--This library provides a more effecient way of writing javascript-->
   <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>

<body>
  <form>
  <input id ="checkboxId" type ="checkbox">Add new Loan</input><br>

    <label style="display:none">Enter value:</label>
    <input id="textboxId" style="display:none" type ="text"/>
  <button style="display:none" type ="submit">Click Me</button>
</form>

<script>

//In between your script tags add the following JQuery code

//This will check if the check box is clicked
$('#checkboxId').click(function () {

  //if checkbox is clicked then make the textbox required
  if ($('#checkboxId').prop('checked'))
  {
    $("#textboxId").prop('required',true);
        $("#textboxId").slideDown();
        $("label").slideDown();
        $("button").slideDown();
  } else {
    //else do not make the textbox required
    $("#textboxId").prop('required',false);
      $("#textboxId").slideUp();
      $("label").slideUp();
      $("button").slideUp();
  }

});
</script>
</body>
</html>