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

Javascript 如何验证该字段是否大于仅当选中特定按钮时的值

Javascript 如何验证该字段是否大于仅当选中特定按钮时的值,javascript,jquery,forms,validation,button,Javascript,Jquery,Forms,Validation,Button,对不起,我的问题很愚蠢,但我不熟悉JQuery。我想检查只有当用户单击特定按钮时,某个字段值是否大于其他字段值。我的表单上有两个按钮,用于提交具有不同按钮值的值。 这是我不知道如何编写的JavaScript代码: $(document).ready(function(){ // If click on DKV button, check thaht field prix_total > litrage // else if click on GR, do not check

对不起,我的问题很愚蠢,但我不熟悉JQuery。我想检查只有当用户单击特定按钮时,某个字段值是否大于其他字段值。我的表单上有两个按钮,用于提交具有不同按钮值的值。 这是我不知道如何编写的JavaScript代码:

$(document).ready(function(){
    // If click on DKV button, check thaht field prix_total > litrage
    // else if click on GR, do not check that prix_total > litrage
    $("#creer_carburant_DKV").click(function(){
        var price = $("#prix_total").val();
      var liter = $("#litrage").val();
        if (price > liter){
        // send the form
      }else{
        alert("The price must be greater than liter");
      }

     $("#creer_carburant_GR").click(function(){
        // If prix_total is not set, define to 0
    });

这是一个关于JSFIDLE的工作示例:

您的if语句比较的是两个字符串而不是数字。你可以把价格和公升换算成数字,这样你就没事了

if (Number(price) > Number(liter)) {

您的if语句比较的是两个字符串而不是数字。你可以把价格和公升换算成数字,这样你就没事了

if (Number(price) > Number(liter)) {

当出现问题时,您需要停止提交表单。使用其他事件,而不是单击按钮。 像这样:

<script>
$(document).ready(function(){
    $("#creer_carburant_DKV").click(function(){
        var price = $("#prix_total").val();
        var liter = $("#litrage").val();
        if (Number(price) > Number(liter)){
            // send the form
        }else{
            alert("The price must be greater than liter");
            event.preventDefault(); // This causes the submit event to be canceled
        }
    });
});
</script>

$(文档).ready(函数(){
$(“#creer#u carbonant_DKV”)。单击(函数(){
var价格=$(“#prix_total”).val();
var升=$(“#litrage”).val();
如果(数量(价格)>数量(升)){
//寄表格
}否则{
警告(“价格必须大于升”);
event.preventDefault();//这会导致取消提交事件
}
});
});

出现问题时,您需要停止提交表单。使用其他事件,而不是单击按钮。 像这样:

<script>
$(document).ready(function(){
    $("#creer_carburant_DKV").click(function(){
        var price = $("#prix_total").val();
        var liter = $("#litrage").val();
        if (Number(price) > Number(liter)){
            // send the form
        }else{
            alert("The price must be greater than liter");
            event.preventDefault(); // This causes the submit event to be canceled
        }
    });
});
</script>

$(文档).ready(函数(){
$(“#creer#u carbonant_DKV”)。单击(函数(){
var价格=$(“#prix_total”).val();
var升=$(“#litrage”).val();
如果(数量(价格)>数量(升)){
//寄表格
}否则{
警告(“价格必须大于升”);
event.preventDefault();//这会导致取消提交事件
}
});
});

感谢您的回答,但即使我投到数字,表格也会发送。感谢您的回答,但即使我投到数字,表格也会发送。