-1

私はphp、jquery、ajaxの初心者です。ボタンのクリック時にテキストボックスを動的に追加および削除するコードがjqueryにあります。動的に作成されたテキスト ボックスの値の値を次のページに投稿し、それらの値を次のページに表示したいと考えています。事前に感謝します。

以下のコードを貼り付けました..

リンクhttp://jsfiddle.net/NSePb/1/も確認できます

    $(document).ready(function () {

            var counter = 1;

            $("#addButton").click(function () {

            if(counter>7){
                alert("Only 7 textboxes allow");
                return false;
                }   

        var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + counter);

        newTextBoxDiv.after().html('<label>Product #'+ counter + ' : </label>' +
      '<input type="text" size="22" name="product' + counter + 
      '" id="product' + counter + '" value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n\
          <label>Quantity #'+ counter + ' : </label>' +
      '<input type="text" size="1" name="qty' + counter + 
      '" id="qty' + counter + '" value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n\
          <label>Rate #'+ counter + ' : </label>' +
      '<input type="text" size="2" name="rates' + counter + 
      '" id="rates' + counter + '" value="" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n\
          <label>Total #'+ counter + ' : </label>' +
      '<input type="text" size="3" name="total' + counter + 
      '" id="total' + counter + '" value="" > ');

        newTextBoxDiv.appendTo("#TextBoxesGroup");


        counter++;
        });

        $("#removeButton").click(function () {
        if(counter==0){
        alert("No more textbox to remove");
        return false;
        }   

        counter--;

        $("#TextBoxDiv" + counter).remove();

        });
4

2 に答える 2

0

テキスト ボックスを動的に作成している場合、テキスト ボックスの名前は動的であると想定していますが、ここでは表示できません。したがって、動的フィールド名を次のように配列に格納します。

$request = array(#field names#) // comma separated
$requestJson = json_encode($request);

合格

$requestJson as an hidden field in the form and submit the form.

次のページ、つまりアクションページで隠しフィールドを受け取り、それをデコードします

$requestArr = json_decode($_REQUEST['requestFields']);

配列をループし、値を次のように受け取ります

for($i=0; $i < count($requestArr); $i++)
{
        $value = $requestArr[$i]; // get the field name
        $content = $_REQUEST[$value]; // get the input value
            $fetchedResult = array($content); // Store the text field value 

}

Now your $fetchedResult will have the values to your future usage.

更新 - コメントに関する最新の質問に従って

var queryArr = [];

newTextBoxDiv.appendTo("#TextBoxesGroup"); // after this line add 
queryArr.push(product' + counter + '); // alert OR print in console to check the array values as and when the button is clicked.

$("#TextBoxDiv" + counter).remove(); //after this add
var removeItem = product' + counter + ' // #removed id#;
y = jQuery.grep(queryArr, function(value) {
  return value != removeItem;
});

$('<input>').attr({
    type: 'hidden',
    id: 'foo',
    name: 'bar',
    value : queryArr
}).appendTo('form');
于 2013-04-23T05:08:11.793 に答える