0

私がやりたいことは、ユーザーがデータをファイリングするときにテキスト ボックスを追加するかどうかを選択できるようにすることです。

これまでのところ、私はこのコードを持っています

http://code.jquery.com/jquery-1.5.1.js

これは私が得たコードです。

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.6.2.js'></script>
  <link rel="stylesheet" type="text/css" href="/css/normalize.css">


  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>
    .Delete{color:#ff0000;}
.Add {color:green;}
.Retrieve{color:blue;} 
  </style>

<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(function(){

    $('.Delete').live('click',function(e){
    $(this).parent().remove();
    });

    $('.Add').live('click',function(e){
        $('.Option:last').after($('.Option:last').clone());
    });

    $('.Retrieve').live('click',function(e){
        $('.Option input').each(function(i,e){
        alert($(e).val()); //Alerts all values individually
        });
    });

});
});//]]>  

</script>

</head>
<body>
  <div class='Option'>
    <input type='text' name='txtTest'/>
    <input type='text' name='txtTest'/>
    <input type='text' name='txtTest'/>
    <span class='Delete'>Delete</span></div>

    <br/><br/>
    <span class='Add'>Add Option</span>
    <br/><br/>
    <span class='Retrieve'>Retrieve Values</span> 
</body>
</html>

そのコードにより、テキストボックスを追加して削除することができますが、私の問題は、PHP GETまたはPHP POSTを使用してこれらのテキストボックスの値を取得しようとしており、可能であれば、おそらく5セットのテキストボックスを追加した後に制限を設定することですユーザーはそれ以上追加できません。

4

2 に答える 2

2

PHP に投稿するには、HTML をform要素でラップするだけです。

<form method="GET" action="myPage.php">
    <div class='Option'>
        <input type='text' name='txtTest'/>
        <input type='text' name='txtTest'/>
        ...
    </div>
</form>

次に、.Option追加できる要素の数を制限するには、要素の数をカウントするグローバル変数を宣言するだけです.Option

var optionCount = 1;

$('.Delete').live('click',function(e){
    $(this).parent().remove();
    optionCount--; /* Decrease optionCount. */
});

$('.Add').live('click',function(e){
    if (optionCount < 5) { /* Only if optionCount is less than 5... */
        $('.Option:last').after($('.Option:last').clone());
        optionCount++; /* Increase optionCount. */
    }
});
于 2013-11-02T09:37:04.160 に答える
1

add イベントの入力数を確認する必要があります。フォームタグを使用してサーバーにデータを転送し、GET または POST を使用してフォームタグに「メソッド」属性を設定する必要もあります

    <html>
    <body>
    <head> 
<script type='text/javascript'> $(function(){
 $('.Add').live('click',function(e){
       if($('.option input').length < 5)
       {
          $('.Option:last').after($('.Option input:last').val('').clone());
       }
          // Other code
          // ...
 }
    });
</script>
</head>
    <form action="welcome.php" method="post">
    <input type='text' name='txtTest'/><br>
    <input type='text' name='txtTest'/><br>
    <input type="submit">
    </form>

    </body>
    </html>
于 2013-11-02T09:58:17.233 に答える