2017-10-08 117 views
0

我有以下jQuery的脚本,它的工作原理的yii2如何设置ID为AJAX

 <script> 
 
      $(function() { 
 
       $(".comment_button").click(function() { 
 
        var element = $(this); 
 
        var test = $("#_id_").val(); 
 
        var dataString = '_id_=' + test; 
 
        if (test == '') 
 
        { 
 
         alert("No record, no action...!"); 
 
        } else 
 
        { 
 
         $.ajax({ 
 
          type: "POST", 
 
          url: '<?=\Yii::$app->urlManager->baseUrl?>/insert.php', 
 
          //url: 'insert.php', 
 
          data: dataString, 
 
          cache: false, 
 
/* 
 
          success: function (html) { 
 
           $("#display").after(html); 
 
           document.getElementById('_id_').value = ''; 
 
*/ 
 
          } 
 
         }); 
 
        } 
 
        return false; 
 
       }); 
 
      }); 
 
     </script> 
 
    

它正常工作之外,是因为我有下面的HTML输入框:

<textarea cols="30" rows="2" style="width:480px;font-size:14px; font-weight:bold" id="_id_" maxlength="145" ></textarea> 

正如你所看到的,jquery将采用id = _id _来使用AJAX。 我试图实现与yii2同样的事情,但我得到以下错误:

bewerber_update?id=2:1925 Uncaught TypeError: Cannot set property 'value' of null 
 
    at Object.success (bewerber_update?id=2:1925) 
 
    at fire (jquery.js:3187) 
 
    at Object.fireWith [as resolveWith] (jquery.js:3317) 
 
    at done (jquery.js:8757) 
 
    at XMLHttpRequest.<anonymous> (jquery.js:9123)

这个错误将被抛出,因为我却不知道,如何正确地在设置ID yii2。我想这样的,但是这显然是错误的方法:

<?= 
 
    $form->field($model, 'beurteilung_fachlich')->widget(\dosamigos\ckeditor\CKEditor::className(), [ 
 
     'id'=>'_id_', 
 
     'options' => ['rows' => 1], 
 
     'preset' => 'full' 
 
    ]) 
 
    ?>

任何想法,如何设置ID以正确的方式?

回答

1

你应该通过idoptions

<?= $form->field($model, 'beurteilung_fachlich')->widget(\dosamigos\ckeditor\CKEditor::className(), [ 
    'options' => ['rows' => 1, 'id' => '_id_'], 
    'preset' => 'full' 
]) ?>