Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

jquery - How to validation of input text field in ajax?

My javascript code is like this :

$.ajax({
    url:base_url+"agent_control/manage_booking/get_pax",
    type: "post",
    data: {id: id},
    success:function(data) {
        App.unblockUI();
        var code = JSON.parse(data);
        var isitable;
        var j;
        for(var i = 0; i < code.length; i++) {
            j = i + 1;
            isitable += '<tr>';
            isitable += '<td>'+code[i].transactionpax_title+' '+code[i].transactionpax_firstname+' '+code[i].transactionpax_lastname+'</td>';
            isitable += '<td><input type="text" id="tiket_number_'+i+'" class="form-control required" /></td>';
            isitable += '</tr>';
        }
        isitable += '<tr><td>&nbsp;</td><td><input type="button" value="Submit" class="btn blue dropdown-toggle submit_tiket_number" id="'+id+'" data="'+code.length+'" /></td></tr>';
        $("#data_tiket_number").find("tbody").html(isitable);
        $('#data_tiket_number').modal('show');
    }
});

Look here : <input type="text" id="tiket_number_'+i+'" class="form-control required" />

I had add required in text input. But it's not working If the text input is empty, when I click submit there was no message required

Any solution to solve my problem?

Thank you very much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to do three things:

  1. make sure you have form around your table
  2. move required out of the class
  3. change your button type from button to submit

Check demo - Fiddle

for(var i = 0; i < code.length; i++) {
    j = i + 1;
    isitable += '<tr>';
    isitable += '<td>'+code[i].transactionpax_title+' '+code[i].transactionpax_firstname+' '+code[i].transactionpax_lastname+'</td>';
    isitable += '<td><input type="text" id="tiket_number_'+i+'" class="form-control" required/></td>';
    isitable += '</tr>';
}
isitable += '<tr><td>&nbsp;</td><td><input type="submit" value="Submit" class="btn blue dropdown-toggle submit_tiket_number" id="'+id+'" data="'+code.length+'" /></td></tr>';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...