如何在yii中使用append


how to use append in yii

我在yii中创建了一个表单,并尝试使用append方法。在所述表单中,用户选择项目并输入金额。如果他们点击按钮,它将调用函数。在输出中,它显示"对象文本"。我该如何取回它?

<script type="text/javascript">
function myfunction ()
		 {
            var newElem = document.createElement ("div");
            newElem.innerHTML =document.createTextNode(amount);
            newElem.style.color = "red";
            var container = document.getElementById ("new");
            container.appendChild (newElem);
        }
</script>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'travel1-form',
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>
	<p class="note">Fields with <span class="required">*</span> are required.</p>
    <div class="row">
    <?php echo $form->labelEx($model,'name'); ?>
	<?php echo $form->textField($model,'name',array('name'=>'name'),array('class'=>'addtocarts')); ?>
	<?php echo $form->error($model,'name'); ?>
	</div>
    <div class="row">
    <?php echo $form->labelEx($model,'Seattype'); ?>
	<?php echo $form->dropDownList($model,'seattype',
			  array('S' => 'Sleeper', 'M' => 'Semi-sleeper','A'=>'Seater'),
              array('empty' => '(Select Type)','name'=>'seattype'));?>
 	<?php echo $form->error($model,'seattype'); ?>
	</div>
 <div class="row">
    <?php echo $form->labelEx($model,'amount'); ?>
	<?php echo $form->textField($model,'amount',array('name'=>'amount')); ?>
	<?php echo $form->error($model,'amount'); ?>
	</div>
   
</div>
    <?php echo CHtml::submitButton('Submit',array('name'=>'submit'))?>
     <?php echo CHtml::htmlButton('AddtoCart',array('name'=>'addtocart','onclick'=>'myfunction();'))?>
      <div id="new">
    </div>
     
        <?php $this->endWidget(); ?>
</div>

请更改如下js函数,

<script>
function myfunction ()
         {           
           var container = document.getElementById ("new");
           var newElem = document.createElement ("div");
           newElem.innerHTML =$('#amount').val();
           newElem.style.color = "red";
           container.appendChild (newElem);
        }
</script>

这个数量textfield应该包括"id"

<div class="row">
    <?php echo $form->labelEx($model,'amount'); ?>
    <?php echo $form->textField($model,'amount',array('name'=>'amount','id'=>'amount')); ?>
    <?php echo $form->error($model,'amount'); ?>
    </div>

您需要从文本字段中获取amount,然后在创建文本节点后,将其附加到newElem

function myfunction() {
    var amount = document.getElementById('amount').value;  // amount value from textfield
    var newElem = document.createElement("div");
    
    newElem.appendChild(document.createTextNode(amount));
    /* You can also use innerHTML instead of the above line like this newElem.innerHTML = amount; */
    
    newElem.style.color = "red";
    var container = document.getElementById("new");
    container.appendChild(newElem);
}
<div id="new"></div>
<input type="text" id="amount" />
<button onclick="myfunction()">Click</button>