Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

javascript - Enable submit button only when all fields are not empty - type > file & select & input

<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU=" crossorigin="anonymous"></script>
        
<script type="text/javascript">
       required = function(fields) {
            var valid = true;
            fields.each(function () { // iterate all
                var $this = $(this);
                if (($this.is(':checkbox') && !$this.is(":checked")) || // checkbox
                    (($this.is(':text') || $this.is('textarea')) && !$this.val()) || // text and textarea
                    ($this.is(':radio') && !$('input[name='+ $this.attr("name") +']:checked').length)) { // radio
                    valid = false;
                }
            });
            return valid;
        }

        validateRealTime = function () {
            var fields = $("form :input:not(:hidden)"); // select required
            fields.on('keyup change keypress blur', function () {
                if (required(fields)) {
                    {submit.disabled = false} // action if all valid
                } else {
                    {submit.disabled = true} // action if not valid
                }
            });
        }

        validateRealTime();
</script>
<form action="" method="post" id="submitform" />

Title:
<input type="text" name="title">

Select:
<select>
    <option value="">aaa</option>
    <option value="1">bbb</option>
    <option value="2">ccc</option>
</select>
<br/>
 <label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile"> 
<br>
Description:
<textarea name="description"></textarea> 

<br/>
Category:
<ul class="list:category categorychecklist form-no-clear" id="categorychecklist">

<li id="category-19"><label class="selectit"><input type="radio" id="in-category-19" name="category" value="19"> Animation</label></li>

<li id="category-20"><label class="selectit"><input type="radio" id="in-category-20" name="category" value="20"> Anime</label></li>

</ul>

<input type="submit" value="Submit Topic" class="button-primary" name="submit" id="submit" disabled="disabled" /> 

</form>
question from:https://stackoverflow.com/questions/65943680/enable-submit-button-only-when-all-fields-are-not-empty-type-file-select

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If u add id on every input, u can use next js vanilla code, and add onchange function on form.

<head>
<script>

    function validate(){
        var submit = document.getElementById('submit');
        var title = document.getElementById('title').value === ""? false: true; 
        var file = document.getElementById("myfile").value === ""? false: true;
        var val19 = document.getElementById("in-category-19").checked;
        var val20 = document.getElementById("in-category-20").checked;
        var checked = val19 || val20;
        
        var textArea = document.getElementById("textarea").value===""?false: true;
        var filled =(checked && title&& textArea && file);
        filled? submit.disabled=false: submit.disabled=true;
    }
    
</script>
</head>

  <body><form onchange="validate();" action="" method="post" id="submitform" /> Title:
<input type="text" id="title" name="title"> Select:
<select>
  <option value="">aaa</option>
  <option value="1">bbb</option>
  <option value="2">ccc</option>
</select>
<br/>
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
<br> Description:
<textarea id ="textarea" name="description"></textarea>

<br/> Category:
<ul class="list:category categorychecklist form-no-clear" id="categorychecklist">

  <li id="category-19"><label class="selectit"><input type="radio" id="in-category-19" name="category" value="19"> Animation</label></li>

  <li id="category-20"><label class="selectit"><input type="radio" id="in-category-20" name="category" value="20"> Anime</label></li>

</ul>

<input type="submit" value="Submit Topic" class="button-primary" name="submit" id="submit" disabled=true />

</form>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...