简体   繁体   English

如何实时比较来自 ajax 调用数据库的数据值与 html 输入值

[英]How compare data value get from ajax call database with html input value in realtime

I try to compare two data values.我尝试比较两个数据值。 one value get from database by ajax jquery call通过 ajax jquery 调用从数据库中获取一个值


function marketbuyshow(){

  $.ajax({
          type: "GET",
          url: "/showmarketbuydata",
          dataType: "json",
          success: function (response) {

   $("#balance").html("");
            $.each(response.balancedata,function(key,item){
              $('#balance').append('<span>'+item.mybalance+'</span>');
                      
            });

other one get from input value.另一个从输入值中获取。 I have this form for data input, price / amount / total input我有这个表格用于数据输入,价格/金额/总输入


                  <form>
                          <div class="title"> </div>
                          <div >Avaliable Balance : <span id="balance"></span></div>
                        <div class="mb-3">
                          <label for="">Price</label>
                          <input type="text" id="price" required class="price form-control" />
                        </div>
                        <div class="mb-3">
                          <label for="">Amount</label>
                          <input type="text" id="amount" required class="amount form-control" />
                        </div>
                        <div class="mb-3">
                          <label for="">Total</label>
                          <input type="text" id="total" required class="total form-control" readonly />
                         
                        </div>
                        <button type="submit" id="btnn" class="btn btn-primary marketbuy">Buy</button>
                      </form>  

if total value greater than avaliable balance I want to disable buy button by jquery as follow but this balance value not work.如果总值大于可用余额,我想通过 jquery 禁用购买按钮,如下所示,但此余额值不起作用。 Although show data in frontend, it pass with empty string and cannot compare in real time.虽然在前端显示数据,但它以空字符串传递,无法实时比较。

 let balan = $('#balance').val();
       console.log(balan);
       let total = $('#total').val();
       console.log(total);
if(total >balan){
         console.log($('#total').val());
         $("#btnn").attr("disabled", true);
       }else{
       // console.log("total is less than ");
        $("#btnn").attr("disabled", false);
       }

在此处输入图像描述

How can I get balance value in realtime and can compare whenever total values get in inputform.如何实时获取余额值,并在输入表单中输入总值时进行比较。 How can I do this.我怎样才能做到这一点。 Please someone help me.请有人帮助我。

Span won't have a val().跨度不会有 val()。 If you want to use val(), make it a readonly input.如果要使用 val(),请将其设为只读输入。 You could also use.text() to get what's in the span.您还可以使用 .text() 来获取跨度中的内容。

General idea of solution:解决方案的总体思路:

For real time you might consider using Socket.io (HTML version).对于实时,您可以考虑使用 Socket.io(HTML 版本)。 This way you will create a direct, two-way connection with your Server.这样,您将创建与服务器的直接双向连接。 This way you can push the new value to the client whenever it changes in the backend.这样,您可以在后端更改时将新值推送到客户端。 Anytime you write in the input, you could in the onChange compare the user input with the current value pushed by Socke.io, which will constantly update in real time.任何时候你在输入中写入,你都可以在 onChange 中将用户输入与 Socke.io 推送的当前值进行比较,该值将不断实时更新。

Why do I recommend Socket.io?为什么我推荐Socket.io?

Forgive me, I took apart your entire code and I am aware of that, however, I wanted to make you aware of a slightly different handling of the problem.原谅我,我拆开了你的整个代码,我知道这一点,但是,我想让你知道对问题的处理略有不同。 Socket.io is a clean solution in that the server contacts you only when that value actually changes and so client-side you don't need long polling or anything like that. Socket.io 是一个干净的解决方案,因为服务器仅在该值实际更改时才与您联系,因此客户端您不需要长轮询或类似的东西。

Here's an example of getting the span contents:这是获取跨度内容的示例:

https://jsfiddle.net/642s8nau/2/ https://jsfiddle.net/642s8nau/2/

<form>
  <div class="title"> </div>
  <div >Avaliable Balance : <span id="balance">1000</span></div>
  <div >Another Value : <span id="anotherValue">500</span></div>
  <button type="button" id="getValues" class="btn btn-primary">Compare Values</button>
</form>  
    
$(document).on("click", "#getValues", function (event) {
    alert(Number($("#balance").text()) > Number($("#anotherValue").text()));
});

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

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