Pages

5/17/2012

jQuery : เพิ่ม Input แล้ว Submit


Code ตัวอย่างสำหรับใครที่อยากจะได้เพิ่ม input แบบ add ได้เรื่อยๆแล้วกด Submit แบบ jQuery
Soruce ตามนี้


document).ready(function(){
    var i = $('input').size() + 1;
    $('#add').click(function() {
        $('<div><input type="text" class="field" name="dynamic[]" value="' + i + '" /></div>').fadeIn('slow').appendTo('.inputs');
        i++;
    });
    $('#remove').click(function() {
    if(i > 1) {
        $('.field:last').remove();
        i--;
    }
    });
    $('#reset').click(function() {
    while(i > 2) {
        $('.field:last').remove();
        i--;
    }
    });
    // here's our click function for when the forms submitted
    $('.submit').click(function(){
    var answers = [];
    $.each($('.field'), function() {
        answers.push($(this).val());
    });
    if(answers.length == 0) {
        answers = "none";
    }  
    alert(answers);
    return false;
    });
});



Refer : http://papermashup.com/dynamically-add-form-inputs-and-submit-using-jquery/

Source Code

1 comment:

  1. Merkur Futur Adjustable Safety Razor - Sears
    Merkur Futur Adjustable Safety Razor wooricasinos.info is the perfect balance deccasino of performance, safety, https://septcasino.com/review/merit-casino/ and comfort. Made in Solingen, Germany, this razor ventureberg.com/ has a perfect balance of gri-go.com

    ReplyDelete