YII:通过onclick方法为变量赋值

YII: Assign value to a variable from onclick method

下午好,我已经为这些问题苦苦挣扎了 2 天,我 运行 没时间了...

我有这个模式...

    <div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <?php if ( $modalToOpen == "1") { ?>
        <h4>Aplicar abono a la cuenta #<?php echo $cardInformation->CardNumber ?></h4>
    <?php } ?>
    <?php if ($modalToOpen == "2") { ?>
        <h4>Aplicar cargo a la cuenta #<?php echo $cardInformation->CardNumber ?></h4>
    <?php } ?>

</div>

<div class="modal-body">
    <p>Saldo Actual: <?php echo $lastBalance ?></p>
    <?php echo $modalToOpen ; if($modalToOpen == 1){ ?>
    <?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array('id' => 'inlineForm', 'type' => 'inline', 'htmlOptions' => array('class' => 'well'))); ?>
    <?php echo $form->TextField($auxModel, 'Payment', array('size' => 8, 'maxlength' => 8, 'placeholder' => "$. Abono", 'class' => 'span1')); ?>
    <?php echo $form->TextField($auxModel, 'Coment', array('size' => 250, 'maxlength' => 250, 'placeholder' => "Comentario", 'class' => 'span3')); ?>
    <?php
    $this->widget(
            'bootstrap.widgets.TbButton', array(
            'buttonType' => 'ajaxButton',
            'type' => 'primary',
            'label' => 'Guardar',
            'url' => Yii::app()->createUrl('BusinessTargetCollectionCard/ApplyPayment'),
            'htmlOptions' => array('onclick' => 'showWait();','id' =>'payment-btn'.  uniqid()),
            'ajaxOptions' => array(
                'type' => 'POST',
                'dataType' => 'json',
                'data' => array(
                    'cardNumber' => $cardInformation->CardNumber,
                    'lastBalance' => $lastBalance,
                    'coment' => 'js:$("#BusinessTargetCollectionCardMovement_Coment").val()',
                    'payment' => 'js:$("#BusinessTargetCollectionCardMovement_Payment").val()',
                    'businessTargetCollectionCardId' => $cardInformation->Id,
                    'collectorId' => $cardInformation->CollectorId),
                'success' => 'js:function(data){
                        alert(data.message);
                        if(data.status == "OK"){
                            $("#PaymentModal").modal("hide");
                            hideWait();
                        }
                    }'
            ),
        )
    );
    $this->endWidget(); ?>
    <?php } ?>
    <?php if($modalToOpen == 2){ ?>
    <?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array('id' => 'inlineForm', 'type' => 'inline', 'htmlOptions' => array('class' => 'well'))); ?>
    <?php echo $form->TextField($auxModel, 'BalanceAfter', array('size' => 8, 'maxlength' => 8, 'placeholder' => "Cantidad a aplicar", 'class' => 'span1')); ?>
    <?php
    $this->widget(
            'bootstrap.widgets.TbButton', array(
                'buttonType' => 'ajaxButton',
                'type' => 'primary',
                'label' => 'Guardar',
                'url' => Yii::app()->createUrl('BusinessTargetCollectionCard/ApplyCharge'),
                'htmlOptions' => array('onclick' => 'showWait();', 'id' => 'charge-btn' . uniqid()),
                'ajaxOptions' => array(
                    'type' => 'POST',
                    'dataType' => 'json',
                    'data' => array(
                        'cardNumber' => $cardInformation->CardNumber,
                        'charge' => 'js:$("#BusinessTargetCollectionCardMovement_BalanceAfter").val()'),
                    'success' => 'js:function(data){
                            alert(data.message);
                            if(data.status == "OK"){
                                $("#myModal").modal("hide");
                                hideWait();
                            }
                        }'
                ),
            )
    ); 
    $this->endWidget();
    ?>
    <?php } ?>

</div>

我有这些按钮:

<td><?php
            $this->widget(
                    'bootstrap.widgets.TbButton', array(
                    'label' => 'Aplicar cargo',
                    'type' => 'success',
                    'htmlOptions' => array(
                        'onclick' => I dont know what to do // 'js:$("#modalToOpen").val("1");',
                        'data-toggle' => 'modal',
                        'data-target' => '#myModal',
                    ),
                )
            );
            ?></td>

        <td></td>
        <td><?php
            $this->widget(
                    'bootstrap.widgets.TbButton', array(
                    'label' => 'Aplicar pago',
                    'type' => 'primary',
                    'htmlOptions' => array(
                        'onclick' => I dont know what to do //'js:$("#modalToOpen").val("2");',
                        'data-toggle' => 'modal',
                        'data-target' => '#myModal',
                    ),
                )
            );
            ?></td>

我想做的是根据单击哪个按钮更改 $modalToOpen 的值...因此如果单击按钮 1 或按钮 2,模态的内容将不同...

非常感谢任何帮助

你可以像这样通过data-id传递它

<?php
            $this->widget(
                    'bootstrap.widgets.TbButton', array(
                    'label' => 'Aplicar cargo',
                    'type' => 'success',
                    'htmlOptions' => array(
                        'onclick' =>'js:function(data){
                          var myvalue = $(this).data("id");
                          $(".modal-body #myvalue").val(myvalue);
                              }',
                        'data-toggle' => 'modal',
                        'data-target' => '#myModal',
                        'data-id'=>'1',
                    ),
                )
            );
            ?>

中的一个隐藏或文本中获取该值
<div class="modal-body">
        <p>some content</p>
      <input type="text" name="myvalue" id="myvalue" value=""/>
</div>

希望对您有所帮助。