I'm trying to write a generalised function that validates my field through Jquery Ajax. My problem is, I want to give key names via function input. However in the field_value variable, my field_label parameter stays as "field_label". I want to change it like "email", "username" so i can generate different jsons. So any idea ?
function validate_field(span_id,field_id,field_label,url){
var message_field = $("#" + span_id)
$("#" + field_id).keyup(function(){
if(this.value != this.lastValue){
if(this.timer){
clearTimeout(this.Timer);
开发者_运维问答 }
message_field.html('<img src="/media/img/loader.gif">');
var field_value = {field_label : $("#"+field_id).val()};
var json_response = JSON.stringify(field_value);
this.timer = setTimeout(function(){
$.ajax({
type:'POST',
url:url,
contentType:'application/json; charset=utf-8',
data:json_response,
success:function(msg){
message_field.html(msg);
}
});
});
}
});
}
change this line:
var field_value = {field_label : $("#"+field_id).val()};
to this:
var field_value = {}; field_value[field_label] = $("#"+field_id).val();
精彩评论