简体   繁体   中英

Is there any solution to get span value inside input?

How do i get data from Span class to output in input value? have tried any but not working..

something like this

 <input type="text" value="<span class='total_all_amounts'>00.00</span>">

i just need data from class total_all_amounts show in input type

here the javascript and codes

 <script>
        $('.quantity').on('input', function(){
          var parent = $(this).closest('tr');
          var totalAmt = parseInt(parent.find('.total').val());
          var quantity = parseInt($(this).val());

          parent.find('.total_amount').text(totalAmt*quantity);

          calcul_total_quatities();
        })

        function calcul_total_quatities()
        {
          var total = 0;
          $('.total_amount').each(function(){
            total += parseInt( $(this).text() );
          })
          $('.total_all_amounts').text(total);

          post_data_to_server($('.total_amount').val(),total);
        }
    </script>

and the codes is

<span class="total_amount">100</span>
<input type="hidden" class='total' value="100" />
<input type="number" class='quantity' value="0" name="qty" />

''.

What i need is when i increase the amount from quantity it will live show on input type <input type="text" value=" **total amount will show here** ">

What you should do is add unique IDs (to avoid looping through classes) for your <span> and <input> tags like this:

<div>
  <p>
    Cost: <span id ="costSpan" class="total_amount">100</span>
  </p>
</div>
<div>
  <p>
    Total: <span id ="totalSpan" class="total_amount">0</span>
  </p>
</div>

<input id="hiddenInput" type="hidden" class='total' value="" />
<input id="quantity" type="number" class='quantity' min="0" value="0" name="qty" />

Then use JavaScript to retrieve the inputted value from quantity whenever it is edited using the input listener, multiple the value of quantity with the integer parsed value of total_amount then assigned it to your hidden input's value which in turn, assigns it to your span tag like this:

var quantity = document.getElementById('quantity');
var hiddenInput = document.getElementById('hiddenInput');
var costVal = document.getElementById('costSpan');
var totalVal = document.getElementById('totalSpan');

function updateVal(){
  hiddenInput.value = quantity.value * parseInt(costVal.innerHTML);
  totalVal.innerHTML = hiddenInput.value;
}

quantity.addEventListener("input", updateVal);

jsFiddle with above code: http://jsfiddle.net/AndrewL64/vqson3cd/14/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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