javascript - How to concate function parameter value into object literal property name -
i have following js code (also use jquery):
jquery(function($) { $(".to_manufacturer").click(function() { var checked = $(this).is(':checked'); var man_id = $(this).attr("to_manufacturer_id"); update_is_in_to(man_id, "to_manufacturer", checked) }); $(".to_model").click(function() { var checked = $(this).is(':checked'); var man_id = $(this).attr("to_model_id"); update_is_in_to(man_id, "to_model", checked) }); $(".to_type").click(function() { var checked = $(this).is(':checked'); var man_id = $(this).attr("to_type_id"); update_is_in_to(man_id, "to_type", checked) }); function update_is_in_to (man_id_val, modelname, checked_val) { $.ajax({ url: "/admin/catalog/to/"+modelname+"s/ajax/update_is_in_to", type: "get", data: {id: man_id_val, modelname_is_in_to_val: checked_val}, success: function(text) { //$("#total_count").html(text + " товар(ов)"); }, error: function(){ alert('Ошибка обновления объекта с id = '+man_id_val+' ! Очередная попытка...'); $.ajax(this); }, datatype : "html" }); } });
how can make param modelname_is_in_to_val
composite, first part method param modelname
, second string "_is_in_to_val"
?
i tried modelname + "_is_in_to_val"
received errors. right it?
also not code violate according js conventions?
you'll need use bracket notation , build object outside function:
function update_is_in_to (man_id_val, modelname, checked_val) { var data = {}; data.id = man_id_val; data[modelname + '_is_in_to_val'] = checked_val; $.ajax({ ... data: data, ... }); }
Comments
Post a Comment