2017-03-31 120 views
2

每個input字段對於總體測驗標記(input字段)應該是相關的。我只想設置一個attribute: max這應該是less than or equal to總測驗標記。 我附上快照&我相信它會很清楚。 enter image description here通過JQuery使輸入[數字]字段從屬於另一個輸入[數字]字段

&我還附上我的HTML代碼:

<form action="../controllers/classroom.php" method="POST">  
    <input type="number" class="form-control" id="totalMark" name="totalMark" placeholder="Enter quiz mark" required> 
    <div class="row"> 
    <?php 
     $count=0; 
     $query4="SELECT * FROM enrollments WHERE classId='{$classId}'"; 
     $res4=mysqli_query($connection, $query4); 

     while($row=mysqli_fetch_array($res4)){ 
      $count++; 
    ?> 
      <div class="col-md-2">#<?php echo $count; ?></div> 
      <div class="col-md-6"><?php echo ucwords(getUser($row['userId'])['name']); ?></div> 
      <div class="col-md-4"><input type="number" class="form-control" name="user[<?php echo $row['id']; ?>]" placeholder="Enter marks" required> 
      </div> 
    <?php } ?> 
    </div> 
    <button type="submit" name="quiz-marks" class="btn btn-primary">Submit</button> 
</form> 

我要讓現場驗證。謝謝

回答

1

最簡單的方法就是聽取#totalMark輸入上的change事件,然後更改max屬性。爲簡潔起見,我使用了jQuery,但我相信如果你願意,你可以使用vanilla javascript。

$(function(){ 
 
\t $('#totalMark').change(function(){ 
 
\t \t $('.form-control:not(#totalMark)').prop('max',this.value) 
 
\t }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    <input type="number" class="form-control" id="totalMark" name="totalMark" value="20" placeholder="Enter quiz mark" required> 
 
    <input type="number" class="form-control" max="20" required> 
 
    <input type="number" class="form-control" max="20" required> 
 
    <input type="number" class="form-control" max="20" required> 
 
</form>

+0

你救了我,先生,謝謝 –

相關問題