Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.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_Php_Jquery_Css - Fatal编程技术网

Javascript 当我选择“已完成”下拉选项时,注释字段是必需的

Javascript 当我选择“已完成”下拉选项时,注释字段是必需的,javascript,php,jquery,css,Javascript,Php,Jquery,Css,问题是,javascript不起作用。当我从下拉列表中选择“完成”选项时,需要注释字段。此验证应在“提交”按钮上完成。如果使用任何其他选项,则注释字段不必填写 下面是我的代码:index.php <form class="form-horizontal" method="post" action="new.php"> <!-- Select Basic --> <div class="form-group"> <la

问题是,javascript不起作用。当我从下拉列表中选择“完成”选项时,需要注释字段。此验证应在“提交”按钮上完成。如果使用任何其他选项,则注释字段不必填写

下面是我的代码:index.php

    <form class="form-horizontal" method="post" action="new.php">

    <!-- Select Basic -->
    <div class="form-group">
      <label class="col-md-4 control-label" for="selectbasic">Status</label>
      <div class="col-md-4">
        <select id="status" name="status[]" class="form-control" >
          <option value="Pending">Pending</option>
          <option value="Work in process">Work in process</option>
          <option value="Completed">Completed</option>
        </select>
      </div>
    </div>

    <!-- Text input-->
    <div class="form-group">
      <label class="col-md-4 control-label" for="textinput">Comment</label>  
      <div class="col-md-4">
      <input id="commentss" name="comment[]" type="text" placeholder="" class="form-control input-md" /> 
      </div>
    </div>
<div class="col-md-8 col-sm-12 col-24">
    <div class="input_fields" style="color:black">
         <button class="add_field btn " onclick="incrementValue()" >Add More</button>
         <div>
         <input type="text" name="mytextt[]" hidden="" ></div>
</div>
</div>

    <button id="save_btn" name="save_btn" type="submit" onclick="validate();" class="btn btn-success" style="width: 10em;margin-left:10px">Save</button>
    </form>
javascript:

<script type="text/javascript">
  function validate()
  {
    var input=document.getElementById("#status");
    var comm=document.getElementById('#commentss')
    var inputelement=input.value;
    if(inputelement=="Completed")
    {
      comm.required=true;
    }

  }
</script>

您正在使用输入上的哈希符号调用getElementById。在本例中,状态和注释

要按id选择,您需要在不使用哈希的情况下进行调用

document.getElementById('status');
哈希前缀用于querySelector,并表示该选择将是一个id选择器


文档查询选择器“状态”

您正在使用输入上的哈希符号调用getElementById。在本例中,状态和注释

要按id选择,您需要在不使用哈希的情况下进行调用

document.getElementById('status');
哈希前缀用于querySelector,并表示该选择将是一个id选择器

文档查询选择器“状态”

删除onclick=validate;从按钮,并将此代码添加到脚本标记中:

$(document).ready(function () {
        $("#save_btn").click(function () {

            if ($("#status").val() == "Completed") {
                $("#commentss").attr("required", "required");
            }
        });
});
删除onclick=validate;从按钮,并将此代码添加到脚本标记中:

$(document).ready(function () {
        $("#save_btn").click(function () {

            if ($("#status").val() == "Completed") {
                $("#commentss").attr("required", "required");
            }
        });
});

我在这个格式的document.getElementById'status中使用了javascript中的id。但当我选择状态为completed@Daniel LaneI have take id in javascript in this format document.getElementById'status'时,javascript并没有按照要求使用注释字段。当我选择状态为completed@Daniel LaneUse@DanielLane solution并更改var inputelement=input.value时,javascript仍然没有按照要求使用注释字段;to var inputelement=input.value;由于value不是一个函数,我已经更新了我的代码,你可以检查一下。我有添加更多按钮,其中包含下拉列表和注释字段。这个脚本在添加更多中不起作用button@PhilSYou现在混合使用普通Javascript和jQuery,如果加载jQuery库,还可以利用jQuery进行验证。“添加更多”按钮正在添加一个id为[]的选择项,该id无效,javascript无法引用它,id属性只能包含数字、字母、下划线、冒号和句号,并且必须是唯一的。请使用@DanielLane solution并更改var inputelement=input.value;to var inputelement=input.value;由于value不是一个函数,我已经更新了我的代码,你可以检查一下。我有添加更多按钮,其中包含下拉列表和注释字段。这个脚本在添加更多中不起作用button@PhilSYou现在混合使用普通Javascript和jQuery,如果加载jQuery库,还可以利用jQuery进行验证。“添加更多”按钮正在添加一个id为状态[]的选择项,该id无效,javascript无法引用它,id属性只能包含数字、字母、下划线、冒号和句号,并且必须是唯一的。