0

我有一个表格,用户可以在其中发送一定金额的礼物,我希望他们在发送礼物时被告知礼物的费用是这样的。我设法打开一个对话框,但当用户单击“是”时无法处理 ajax 提交。它没有传递给服务器。但是,当我在没有对话框的情况下提交时,它工作正常。请帮忙。提前致谢!这是脚本:

<div id="gift_sent" style="float: left; width: 370px; margin: 5px 0 5px 0; display: none;"><?php echo ucfirst(__('Gift Sent', true)) ?></div>

<div style="float: left; width: 370px; margin: 50px 0 10px 0;">

<?php // echo $this->Form->create('Gift', array('action' => 'sent/' . $user_obj['User']['id'] . '/' . $gift['GiftPhoto']['id'], 'div' => false, 'id' => "submit_".$gift['GiftPhoto']['id'])); ?>
<?php echo $this->Form->create('Gift', array('div' => false, 'id' => "submit_".$gift['GiftPhoto']['id'])); ?>

<?php echo $this->Form->input('user_id', array('type' => 'hidden', 'value' => $user_obj['User']['id'])); ?>
<?php echo $this->Form->input('sender_id', array('type' => 'hidden', 'value' => $user_object['id'])); ?>
<?php echo $this->Form->input('gift_id', array('type' => 'hidden', 'value' => $gift['GiftPhoto']['id'])); ?>

<?php echo $this->Form->input('description', array('label' => 'Greetings, 240 characters max', 'rows' => '5', 'style' => 'width: 350px;', 'div' => false )); ?>

<div class="clr"></div>

<?php echo $this->Form->end('Send'); ?>

</div>


<script type="text/javascript">
jQuery(document).ready(function() {

var link = "<?php echo $html->url("/gifts/sent/" . $user_obj['User']['id'] . '/' . $gift['GiftPhoto']['id']) ?>";

$( "#submit_<?php echo ($gift['GiftPhoto']['id']); ?>" ).submit(function( event ) {

event.preventDefault();

$("#dialog-confirm").dialog('open');
return false;
});

$("#dialog-confirm").dialog({
  resizable: false,
  height:190,
  autoOpen: false,
  width: 330,
  modal: true,
    buttons: {
            "Yes": function() {
$.ajax({
cache: false,
type: "POST",
data: $(this).serialize(),
url: link,
// url: "<?php echo $html->url("/gifts/sent/" . $user_obj['User']['id'] . '/' . $gift['GiftPhoto']['id']) ?>",
success: function() {
    $('#gift_sent').show();
    $(':input', '#submit_<?php echo ($gift['GiftPhoto']['id']); ?>')
    .not(':button, :submit, :reset, :hidden')
    .val('');
    }
        });
            $(this).dialog("close");
            },

            "No": function() {
            $(this).dialog("close");
            }
    }
});

});
</script>



<div id="dialog-confirm" title="Send a Gift">
<p>This Gift Cost <?php echo ($gift_cost); ?> Points to send. You sure you want to continue?</p>
</div>

这是一个没有对话框的工作副本:

<script type="text/javascript">
jQuery(document).ready(function() {

$('#submit_<?php echo ($gift['GiftPhoto']['id']); ?>').bind('submit', function(event) {
$.ajax({
    cache: false,
    type: "POST",
    url: "<?php echo $html->url("/gifts/sent/" . $user_obj['User']['id'] . '/' . $gift['GiftPhoto']['id']) ?>",
    data: $(this).serialize(),

    success: function() {
    $('#gift_sent').show();
    $(':input', '#submit_<?php echo ($gift['GiftPhoto']['id']); ?>')
    .not(':button, :submit, :reset, :hidden')
    .val('');
    }
});
return false; 
});

});
</script>
4

1 回答 1

0

HI you've got some errors in your code:

  1. button Yes is quoted and No isnt
  2. You've got two binding on the created form on submit watch:

    //every form in your code
    $('form').submit(function () {
        if (!submit) {
            $("#dialog-confirm").dialog('open');
            return false;
        }
    });
    
    //concrete one
    $('#submit_<?php echo ($gift['GiftPhoto ']['id ']); ?>').bind('submit',...);`
    

Fix this and run console in your browser and watch if is there a problem. And its not recommend to mix PHP and JS I looks really bad, try use data-xxx attribute on your html elements and get there by jQuery $().attr.

于 2013-11-04T22:04:48.283 回答