简体   繁体   English

如果值大于一个PHP javascript,则阻止提交

[英]Block submit if value greater than one php javascript

I want to disable the submit if the sum of all select values is bigger than one. 如果所有选择值的总和大于一个,我想禁用提交。 And pass if the value < 1. 如果值<1,则通过。

    <?php 
$options = array( "0.00","0.05","0.10","0.15","0.20","0.25","0.30","0.35","0.40","0.45","0.50","0.55","0.60","0.65","0.70","0.75","0.80","0.85","0.90","0.95","1" ); // ◄■■ OPTIONS ARE STATIC (ALWAYS THE SAME).
while ( $row_menuid = mysqli_fetch_array( $dat_menuid ) ) // ◄■ DISPLAY 
    <SELECT>
    s.
{ echo "
<select name='corp_resp&{$row_menuid['menuId']}&{$_SESSION['UtilizadorID']}&{$dateTime}&{$toEchosave}'>\n"; // ◄■■ CORP_RESP&1,CORP_RESP&2.
 foreach ( $options as $opt ) // ◄■■ DISPLAY OPTIONS.
     echo "<option value='$opt'>$opt</option>\n";
 echo "</select>
\n"; // ◄■■ SELECT END.
}
?>
<br>
<br>
<br>
<br>
<script>
    $('select').change(function(){
        var sum = 0;
        $('select :selected').each(function() {
            sum += Number($(this).val());
        }
                                  );
        $("#sum").html(sum);
        if (sum > 1) {
            alert('Block!')
        }
        else {
            alert('Give the value !')
        }
    }
                      );
</script>
<?php
if ($idfilho == 1) 
{
    echo "<td align=\"center\"  bgcolor='FFFFFF'>   <a href='avaliacoes2.php?menuId=".$id."'>Seleccionar</a> </td>";
}
else
{
    echo "";
}
echo    '</tr>';
}
}
echo"</table>";
echo "<div id='sum'>SUM OF SELECTED OPTIONS</div>";
echo "<br><br><br>";
echo ('<input type="submit"  value="Submeter" class="link-style2"  />'); ?>

The variable to get the value in each select option value is working but i want to get only in the final sum and compare. 在每个选择选项值中获取值的变量正在工作,但我只想获取最终的总和并进行比较。 If sum > 1 then block the submit if not then you can submit the value. 如果sum> 1,则阻止提交,否则可以提交值。 I was trying with alerts but is not the correct way to do. 我正在尝试使用警报,但不是正确的方法。

Thank you. 谢谢。

<script>
    var sum = 0;

    $('select').change(function(){
        $('select :selected').each(function() {
            sum += Number($(this).val());
        });

        $("#sum").html(sum);
    }

    $('form').on('submit', function(e) {
        if(sum > 1) {
            e.preventDefault();
        }
    });
</script>

Here, you need to make the variable sum global and add an event handler submit of form element. 在这里,您需要使变量sum全局变量,并添加一个form元素的事件处理程序submit In the event handler, you can check the condition if the sum is greater than 1 or not. 在事件处理程序中,您可以检查条件sum是否大于1 If it is greater than 1 , you can prevent the form from being submitted by e.preventDefault() which stops the event from running. 如果大于1 ,则可以阻止e.preventDefault()提交表单,这将阻止事件运行。

You can also provide alert box or do something when it is exceeds 1 like this one: 您还可以提供警报框,或者当警报框超过1时执行以下操作:

$('form').on('submit', function(e) {
    if(sum > 1) {
        e.preventDefault();
        alert("Blocked!");
    }
});

Hope it helps! 希望能帮助到你!

Attach a submit event handler to the form in question, then within that handler use code much like you've shown for your change event. 将提交事件处理程序附加到有问题的表单上,然后在该处理程序内使用代码,就像为更改事件显示的那样。 If the sum is too big cancel the submit and show a message, otherwise do nothing: 如果总和太大,请取消提交并显示一条消息,否则什么也不做:

$('#idOfYourFormHere').on('submit', function(e) {
  var sum = 0;
  $('select :selected').each(function() {
    sum += Number(this.value);
  });
  $("#sum").html(sum);

  if (sum > 1) {
    e.preventDefault();
    alert('You cannot submit unless the sum of your selections is less than or equal to 1.');
  } // else do nothing and submit will continue
});

Note that you don't need the change handler unless you want to show the running total, just recalculate the total when the user tries to submit. 请注意,除非要显示运行总计,否则不需要更改处理程序,只需在用户尝试提交时重新计算总计即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM