1

こんにちは私は複数の値を保存し、ajaxを使用してデータベースを更新する必要があります。私はYiiフレームワークに取り組んでいます。

最初は、ajaxをjsonとして使用してデータを送信する必要がありますが、結果が間違っています。

ライブコード: http: //jsfiddle.net/kxJwp/2/

私のJavaScriptコードは次のとおりです。

$("#save").live('click', function(){
    var showtimes = [];
    var values = {};
    $('li inputs').each(function() {
        values[$(this).attr('name')] = $(this).val();
    });
    $('li').each(function(i) {
        showtimes[i]=values;
    });
    alert(JSON.stringify(showtimes));
});

Javascript出力:

最後の1つのli値xliの数を出力します

入力:

<li>
    <input name="show_id" class="show_id att" type="hidden" value="" />
    <input name="movie_id" class="movie_id att" type="hidden" value="" />
    <input name="cinema_id" class="cinema_id att" type="hidden" value="" />
    <input name="status" class="status att" type="hidden" value="" />
    <input name="times" class="timesArray att" type="hidden" value="" />
<li>
<li>
    <input name="show_id" class="show_id att" type="hidden" value="" />
    <input name="movie_id" class="movie_id att" type="hidden" value="" />
    <input name="cinema_id" class="cinema_id att" type="hidden" value="" />
    <input name="status" class="status att" type="hidden" value="" />
    <input name="times" class="timesArray att" type="hidden" value="" />
<li>
4

3 に答える 3

3

You are seeing only one row because you have only one object, whereas you need an array of objects, so declare an array first, and then keep adding the objects to it. Something like this:

$("#save").live('click', function(){
  var showtimes = [];    // Create empty javascript array
  var objec={};
  $("li").find("input").each(function(index) {           // Iterate over inputs

    objec=new Object; // to create a new object for every matched element

    objec[$(this).attr('name')]=$(this).val()+'avla';
    showtimes[index]=objec;  // Add object to array
  });

  var json = JSON.stringify(showtimes);
  alert(json);
});

Code explanation: Use the inbuilt index of each(), by passing the index to the function like: function(index){...}.

Edit: objec=new Object; is needed to avoid getting repeated values, because each time the same object was getting added to the array, but with new, a new object is created each time and that is added to the array.

Update: A better way to select the lis would be using a selector such as : $("li:has(input)") and then cycle through the children:

$("#save").live('click', function(){
    var showtimes = [];
    var values = {};
    $("li:has(input)").each(function(i){
        values = new Object;
        $(this).children().each(function(){
            values[$(this).attr('name')]=$(this).val();
        });
        showtimes.push(values);
    });
    alert(JSON.stringify(showtimes));
});

Edit: The output(arrays formed) in both the above samples is different.

Output for code sample 1:

json=[{"show_id":"1avla"},{"movie_id":"2avla"},{"cinema_id":"3avla"},{"status":"4avla"},{"times":"5avla"},{"show_id":"6avla"},{"movie_id":"7avla"},{"cinema_id":"8avla"},{"status":"9avla"},{"times":"0avla"}]

Output for code sample 2:

json=[{"show_id":"1","movie_id":"2","cinema_id":"3","status":"4","times":"5"},{"show_id":"6","movie_id":"7","cinema_id":"8","status":"9","times":"0"}]
于 2012-06-05T20:28:35.590 に答える
0

別の代替方法を使用できます。必要な変更は、ajax を介してフォーム広告を送信するフォームの下にliと を配置することです。また、これinput'に従って入力の名前を変更する必要があります。つまり、角括弧を追加して、PHP で配列として取得できるようにします。JSONは必要ありません

    <form id="myform" action="action.php">
    .....
    <li>
        <input name="show_id[]" class="show_id att" type="hidden" value="" />
        <input name="movie_id[]" class="movie_id att" type="hidden" value="" />
        <input name="cinema_id[]" class="cinema_id att" type="hidden" value="" />
        <input name="status[]" class="status att" type="hidden" value="" />
        <input name="times[]" class="timesArray att" type="hidden" value="" />
    <li>
    <li>
        <input name="show_id[]" class="show_id att" type="hidden" value="" />
        <input name="movie_id[]" class="movie_id att" type="hidden" value="" />
        <input name="cinema_id[]" class="cinema_id att" type="hidden" value="" />
        <input name="status[]" class="status att" type="hidden" value="" />
        <input name="times[]" class="timesArray att" type="hidden" value="" />
    <li>

    ....

</form>

そしてajaxのJavaScript

$("#save").live('click', function(){
    var form = $('#myform');
    $.ajax( {
      type: "POST",
      url: form.attr( 'action' ),
      data: form.serialize(),
      success: function( response ) {
        console.log( response );
      }
    } );


});

次に、コントローラーで、送信されたデータを配列として取得します

print_r($_POST['show_id']);
print_r($_POST['movie_id']);

などなど

于 2012-06-07T08:52:47.760 に答える
0

元の js コードが表示されます

$("tr").find("input.att").each(function() {

ただし、ビューコードは、<li>代わりにを使用していることを示しています<tr>

だからあってはならない$("ul") or $("li")

于 2012-06-03T05:40:44.510 に答える