简体   繁体   中英

How to change span value from a html input which has been appended using ajax call?

I have a modal which is triggered on click and modal data is added by ajax request to my php file. After the ajax call, when the modal shows,I want the span values in my modal to change simultaneously to the input field of my modal.

I have tried this javascript code but it isn't working after the ajax call.

$("#j").on("change keypress input", function() {
     $("#total").value( $("#j").val() );
     var a=($(this).val()*2.37).toFixed(2);
     $("#possible").html(a);

});

My modal:

<div class="modal-body">
    <div id="modal-body" class='form-row has-danger border-bottom mb-30'>

    </div>  
    <div class='col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12 border-bottom form-row mt-15 mb-10'>
       <div class='col-xl-8 col-lg-8 col-md-8 col-sm-8 col-xs-8 text-left'>
           <strong>Total Jobs</strong>
       </div> 
       <div class='col-xl-4 col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right'>
           <strong><span id='total'></span></strong> //want to change value of this based on input field added from php page
       </div>
     </div> 
     <div class='col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12 form-row '>
           <div class='col-xl-8 col-lg-8 col-md-8 col-sm-8 col-xs-8 text-left'>
                <strong>Possible Jobs</strong>
           </div> 
           <div class='col-xl-4 col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right'>
                <strong><span id='possible'></span></strong> //want to change value of this based on input field added from php page
           </div>
      </div>
</div> 

My ajax call:

function job(job_id) {
    $.ajax({
        url : "job.php", //this is my php file where echo for the modal happens
        type : "post",
        dataType:"text",
        data : {
            stake_id: bet_id
        },
        success : function (a){
            $('#modal-body').html(a);//adding echo part to my modal
            $('#RequestModal').modal('show');
        }
} 

我没有全部了解,但我注意到您正在尝试通过val()函数获取span的值,而应使用html()或text()代替,以获取该值并在需要的任何地方使用它

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