开发者

post dynamically generated textbox using foreach loop

i want to post dynamically generated multiple text box using php by foreach loop. for instance i m working on question bank and for post multiple answer from php. here is my code that dynamically generated textbox but now i donot know how to post value of generated textbox thru foreach l开发者_高级运维oop.

$("#questionbank").live('click',function(){
    var mydata = '<div id="questionbank-content" class="page">';
    mydata += '<div class="question"><div class="Label">Question</div><div class="textarea"><textarea id="question" rows="3" cols="30"></textarea></div></div><div class="answer"><div class="Label">Answer</div><div class="textarea"><textarea id="answer" rows="3" cols="30"></textarea></div><div class="option"><input id="a" name="answers" type="radio"></div><span id="add">Add</span>';
    var i=1;
    $('#add').live('click',function(){
         j=i++;
         $(this).after('<div class="Label">Answer</div><div class="textarea"><textarea id="answer + '+ j +'" rows="3" cols="30"></textarea></div><div class="option"><input id="a + '+ j +'" name="answers" type="radio"></div><span id="add">Add</span>');

        //$(this).after('<input id="'+ j +'" type="text" value="'+ j +'"/><span id="add">Add</span>');
        $(this).remove(); 
    });
    mydata += '</div></div>';
    $("#leftcontainer").html(mydata);

});


Consider the following HTML

<div id="start">Start</div>

<div id="container">

</div>

I guess you want to post data from each value of the textbox/textarea by $.post() or $.ajax()

var i=1;
$('#start').one('click',function(){
    j=i++;
   $('#container').append('<input id="answer'+ j +'" type="text" value=""/><span id="add">Add</span>');
    $('#container').after('<div id="submit">Submit</div');
});

$('#add').live('click',function(){
     j=i++;
    $(this).after('<input id="answer'+ j +'" type="text" value=""/><span id="add">Add</span>');
    $(this).next().next().after('');
    $(this).remove(); 
});

$('#submit').live('click',function(){
    var x = $('#container > input');
    var qstring='op=insertquestion'; //additional para if u want to pass any
    $.each(x,function(index,value){
        var id = $(this).attr('id');
        var data = $(this).val();
        qstring += '&'+id+'='+data+'';
    });
    $('#start').before(qstring + '<br/>');
});

After you get the variable qstring just post the string using $.post() method.

Example on JSFIDDLE.net


The easiest way would be to construct the textarea and submit button via jQuery and attach an click handler. Not a complete solution but it should hopefully give you an idea:

$("#add").live("click", function() {
    var textarea = $("<textarea />", { "class": "textarea" });
    var submitButton = $("<button />", { "type": "submit" });

    submitButton.bind("click", function() {
        var text = textarea.val();
        $.post("/path/to/script.php", { text: text });
    });
});

This will create a textarea and a submit button. When invoking the submit button, a HTTP POST is made to script.php with the textarea content as a parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜