-1

我正在尝试制作一个输入表表单以在我的 MVC 页面中输入产品的新记录,这是视图的代码(我正在使用 MongoDB)

<?php
  for ($i = 0; $i <= 1; $i++) {
  <tr>
    <td><?php echo CHtml::activeTextField($modelItem, "[$i]name"); ?></td>
    <td><?php echo CHtml::activeTextArea($modelItem, "[$i]description"); ?></td>
    <td><?php echo CHtml::activeTextField($modelItem, "[$i]price"); ?></td>
  </tr>
<?php } ?>

因此,为了使其成为可扩展的输入表单,我使用:

<script type="text/javascript"> 
$(document).ready(function(){
  addNewRow();
  function addNewRow() {
    var i = $("#listItem tbody tr").size();
    var arr = {};
    $("#listItem tbody tr:last").click(function(){
      if($(this).hasClass('addedRow') == false){
        arr.i = i;
        $("#ADD_PRICE_LIST_ROW").tmpl(arr).appendTo("#listItem tbody");
        addNewRow();
      }
      $(this).addClass('addedRow')
    });
  }
});
</script>

(listItem tbody 是表的id) 那么我要怎么做才能完成下面的功能呢?

<script id="ADD_PRICE_LIST_ROW" type="text/x-jquery-tmpl">
  <tr>
    ????
  </tr>
</script>

抱歉英语不好=.=

4

1 回答 1

1

为此,您需要使用 AJAX:

$.ajax({
    type: 'POST',
    url: '/your/php.php',
    data: {
        post1: $('#element').val(),
        post2: $('#element2').text()
    },
    success: function(result) {
        alert(result);
    },
    error: function() {
        $('#errors').text('Failed to get response from PHP');
    }
});

$.post()如果您不需要太多选项,您也可以使用执行类似的操作:

$.post('/your/php.php', {
    post1: $('#element').val(),
    post2: $('#element2').text()
},
function(result) {
    alert(result);
});

$('#button').on('click', function() { /* $.ajax() or $.post() */ });您可以将它们放在类似或之类的东西$('#textbox1').on('keyup', function() { /* $.ajax() or $.post() */ });中。

在您的 PHP 中,您可以执行以下操作来将其作为任何其他 POST 进行处理:

if(isset($_POST['post1'])) {
    // $result = do stuff with $_POST['post1'];
    echo $result; // The jQuery will grab this as "result" and alert it...
}
于 2012-08-16T19:49:52.357 回答