简体   繁体   English

为什么我的 javascript 代码没有按预期工作

[英]Why my javascript code is not working as expected

I am trying to validate a text field using JavaScript.我正在尝试使用 JavaScript 验证文本字段。 I have an available field and a quantity field.我有一个可用字段和一个数量字段。 I want to give a message below the quantity field when the quantity entered is greater than available.当输入的数量大于可用数量时,我想在数量字段下方给出一条消息。

What I have tried is:我尝试过的是:

<form method="post" action="/saveBid" id="reviewForm">
                                    <input type="hidden" name="_token" value="{{csrf_token()}}" />
                                    <input type="hidden" name="truck_name" value="{{truck_name}}" />
                                    <input type="hidden" name="user_name" value="{{auth_user().first_name}}" />
                                    <input type="hidden" name="seller_id" value="{{seller_id}}" />
                                    <div class="form-group row">
                                        <label class="col-sm-4 col-form-label">Select Milege Gap: </label>
                                        <div class="col-sm-8">
                                        <select class="form-select" name="mileage" id="mileage" onchange="getOption()">
                                            <option>Select </option>
                                            {% for p in product_data %}
                                            <option value="{{p.price}},{{p.number_of_products}},{{p.name}},{{p.id}},{{p.number_of_products_sold}}">{{p.name}}</option>
                                            {% endfor %}
                                        </select>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="available" class="col-sm-4 col-form-label">Available Quantity: </label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" id="available" readonly name="available_qty" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="truck" class="col-sm-4 col-form-label">Price: </label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" readonly id="truck" name="truck" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="qty"  class="col-sm-4 col-form-label"> Quantity: </label>
                                        <div class="col-sm-8">
                                            <input type="text" id="qty" name="qty"  class="form-control"  oninput="checkInput(this);" required />
                                            <p id="qty-msg">
                                            </p>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="t_price"  class="col-sm-4 col-form-label"> Total Price: </label>
                                        <div class="col-sm-8">
                                            <input type="text" readonly id="t_price" name="t_price"  class="form-control" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputBid"  class="col-sm-4 col-form-label">Enter Bid Price</label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" id="inputBid" name="bid" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');"/>
                                        </div>
                                    </div>
                                    <div class="form-group text-center">
                                        <input type="submit" class="btn btn-primary" id="btn" name="send" value="Send" disabled="disabled">
                                    </div>
                                </form>

Javascript code: Javascript代码:

 <script>

function getOption()
    {
        var select = document.getElementById('mileage');
        var option = select.options[select.selectedIndex];
        var opArr = option.value.split(",");
        
        var price=document.getElementById("truck");
        price.value=opArr[0];
        var available=opArr[1]-opArr[4];
        
        document.getElementById("available").value=available;
        if(available<=0)
            {
                alert('Out of Stock');
                document.getElementById("qty").disabled=true;
                document.getElementById("inputBid").disabled=true;
            }
            }
function checkInput(item)
    {
        
        var t_price=document.getElementById("t_price");
        var available=document.getElementById("available");
        var price=document.getElementById("truck");
        var msg=document.getElementById("qty-msg");
        
        if(item.value>available.value)
            { 
                alert("Quantity" +item.value +"Availabe: "+available.value);
                item.value='';
                msg.innerHTML="* Value must be less than Availabe quantity "+available.value;
                msg.style.color="red";
            }
        
        t_price.value=price.value*item.value;
        
    }
</script>   

What I run this code, If the available quantity is 15, and the quantity entered is 1, it is working fine.我运行此代码的内容,如果可用数量为 15,并且输入的数量为 1,则它工作正常。 But when the quantity entered is other than 1, like 2, 3, or anything, it gives the message.但是当输入的数量不是 1 时,如 2、3 或任何其他数量,它会给出消息。 I wish to give a message only when the quantity is greater than 15 like 16, 17, etc. but when I try to enter 12, it is ok.我只想在数量大于 15 时(例如 16、17 等)发出消息,但是当我尝试输入 12 时,就可以了。 when I try to enter 3, 4, etc. It gives the validation message.当我尝试输入 3、4 等时,它会给出验证消息。 Same in the case, if the available quantity is 20, then quantity entered 2 will work.在这种情况下,如果可用数量为 20,则输入 2 的数量将起作用。 but 3 or anything 2 will not work.但是 3 或任何 2 都不起作用。 Why this is happening.为什么会发生这种情况。 Why my javascript is not working properly.为什么我的 javascript 不能正常工作。

My output我的输出

When I try to enter 3, this happens in my output当我尝试输入 3 时,这发生在我的输出中

输出

When I try to enter 2 in the quantity field, it is ok当我尝试在数量字段中输入 2 时,就可以了输出 2

When I try to enter 12, it is working当我尝试输入 12 时,它正在工作

输出 3

When I try to enter 4, it is not working当我尝试输入 4 时,它不起作用

输出 4 I want to show this error message only when the quantity entered is greater than the available qauntity.我只想在输入的数量大于可用数量时显示此错误消息。

As said in the comments, try to convert your input value (string) in numbers before comparing.正如评论中所说,在比较之前尝试将输入值(字符串)转换为数字。 So it'll be something like this :所以它会是这样的:

if(parseInt(item.value)>parseInt(available.value))
{
    ...
}

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

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