javascript - My js code does not work as expected unless i use an alert message -


i have input field :

    <input class="quantityinput" data-index="@table.rows.indexof(row)"  type="number" value="@row[col.columnname]" min="1"/> 

here js code :

$(document).ready(function () {     $(function () {         $(document).on('change', '.quantityinput', function () {             var data = {                 id: $(this).data('index'),                 value: $(this).val()             };             $.getjson("/order/basketsession/", data, function (result) { });             location.reload(true);             alert("hi!");         });     }); 

in controller:

public jsonresult basketsession(int id, int value)         {             (httpcontext.session["basket"] datatable).rows[id][2] = value;              return json(true);         } 

if don't use alert, json func not wok when change value of input field using keybord mouse click increase or decrease 1 one works either if use alert or not(in chrome)

in firefox doesn't work @ all

i hope understand problem

as pointed out, ajax call , doesn't resolve immediately. without digging details, can use done method execute code when getjson returns data.

$(document).ready(function() {     $(document).on('change', '.quantityinput', function () {         var data = {             id: $(this).data('index'),             value: $(this).val()         };         $.getjson("/order/basketsession/", data).done(function(){           location.reload(true);           console.log("hi!");         });     }); }); 

this works because getjson return promise event happen in future. can find more info getjson , promises here.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -