简体   繁体   中英

how to set value to input textbox jquery

I have a table with a text box column and I want to get the old value and set it to the text box onchange .

        <span class="txt_b">
            <input type="number" name="" value="<?php echo $req; ?>" onchange="onChangeTest(this)" onchange="setoldvalue(this)" oldvalue="" class="input_md radious_all innershadow padding_control" id ="tk" style="width:60px;" id="qty_req_<?php echo $i; ?>" onblur="editBucket('<?php echo $row_id; ?>','<?php echo $item['Item']['code']; ?>',$('#qty_war_<?php echo $i; ?>').val(),this.value,'<?php echo $date; ?>',$('#stock_notes_<?php echo $i; ?>').val(),'<?php echo $item['Item']['pack_size']; ?>')"/>
        </span>
    </div>
    <input type="hidden" name="product" id="qty_reqty_" class="product" value="qty_reqty_<?php echo $i; ?>" />
</div>
<script> 
    function getoldvalue()
        {
            $('input').on('focusin', function(){
                console.log("Saving value " + $(this).val());
                $(this).data('val', $(this).val());
            });

            $('input').on('change', function(){
                var prev = $(this).data('val');
                $('input').val(prev);
                console.log(s);
                var current = $(this).val();
                console.log("Prev value " + prev);
                console.log("New value " + current);
            });

I am struggling with setting the value to the text box. How to it by id ?

this is my jquery dialogbox

function editCart(id,code,stock_wh,stock_rq,date,notes,pack){

if((stock_rq%pack) != 0){

    $('<div></div>').appendTo('body')
  .html('<div id="dialog"><h3>The quantity should be multiple of pack size, do you wish to continue with the quantity entered?</h3></div>')
    .dialog({
    modal: true,
    title: 'message',
    zIndex: 10000,
    autoOpen: true,
    width: 'auto',
    resizable: true,
    buttons: {

    Yes: function () {
            doFunctionForYes();
            $(this).dialog("close");
        },

     No: function () {
            doFunctionForNo();

            $(this).dialog("close");
        }
    },

    close: function (event, ui) {
        $(this).remove();
    }
});
$('#msg').hide();

function doFunctionForYes() {

   $('#msg').show();

}
function doFunctionForNo() {
alert("doFunctionForNo");

     });

} 

}

you have some issue on defining html:

  1. can not have 2 id attribute for one input
  2. can not have 2 onchange on input
  3. oldvalue is not standard attribute

you can set data-oldvalue attribute to your input and access that with jQuery

<input type="text" id="qty_req_<?php echo $i; ?>" data-oldvalue="<?php echo $req; ?>" />

<script>
$( document ).ready(function() {
     oldvalue = $("#qty_req_<?php echo $i; ?>").data('oldvalue');
});
</script>

the jquery code is for sample and you can use from this code everywhere which you need

First of you can use $(this).data while saving value, only if you are having html5 . So I've just added it as $(this).attr('data-val') . Now the problem was, you were always having old value as value for input even though you enter anything new, and you were getting current value after you changed the input value to old value . There was also an undefined error when you logged console.log(s) , since s was not defined anywhere.

 $('input').on('focus', function() { $('body').append("<br/>Saving value " + $(this).val()); $(this).attr('data-val', $(this).val()); }); $('input').on('change', function() { var prev = $(this).data('val'); var current = $(this).val(); $(this).val(prev); console.log("Prev value " + prev); console.log("New value " + current); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <span class="txt_b"> <input type="number" name="" value="20" class="input_md radious_all innershadow padding_control" id ="tk" style="width:60px;" id="qty_req_<?php echo $i; ?>"/> </span> </div> <input type="hidden" name="product" id="qty_reqty_" class="product" value="qty_reqty_<?php echo $i; ?>" /> 

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