2017-03-02 58 views
0

我有確認提醒,使用bootbox,js。在bootbox中,用戶可以製作自定義的html表單。但是,我有一些從PHP foreach填充的表單元素/值。我試圖這樣寫,但它不工作。警報沒有被觸發。如何在此javascript行中正確組合PHP結果?

bootbox.confirm({ 
     title : "Testing", 
     message : 
     "<form id='test'>\ 
     <h3>Dept: </h3>\ 
     <table width='100%' cellspacing='0' cellpadding='3px'>\ 
     <?php foreach($dept as $row_dept){ ?> 
     <tr>\ 
      <td align='center' width='5%'><input type='checkbox' name='first_name' /></td>\ 
      <td align='left'>"+<?php $row_dept->dept; ?>+"</td>\ /*If I remove this line, there is no error. But no value is shown*/ 
     </tr>\ 
     <?php }; ?> 
     <tr>\ 
      <td colspan='2'><h3>Disposisi</h3>\<textarea id='disposisi' name='disposisi' class='form-control'></textarea></td>\ 
     </tr>\ 
     </table>\ 
     </form>", 
     buttons : { 
        cancel : { label: '<i class="fa fa-times"></i> Batal' }, 
        confirm : { label: '<i class="fa fa-check"></i> OK'} 
     }, 
     callback: function (result) { 
      if(result == true){ 
       var disposisi = $("#test").find('textarea[name=disposisi]').val(); 
       alert(disposisi); 
      } 
     } 
    }); 
+0

爲什麼你不呼應PHP價值? –

+0

@RanaGhosh我試着用'<?php echo「test」這樣簡單的東西替換表單; ?>'。但是,它不工作。我認爲問題在於'echo' – ashura91

+0

是否顯示控制檯中的任何錯誤? –

回答

0

看來你有一個換行問題。

試試這個 - 反引號`工作在新的瀏覽器 - 以其它方式使用 「... 」+「 ...」 一路

bootbox.confirm({ 
 
    title: "Testing", 
 
    message: `<form id='test'> 
 
     <h3>Dept: </h3> 
 
     <table width='100%' cellspacing='0' cellpadding='3px'> 
 
     <?php foreach($dept as $row_dept){ ?> 
 
     <tr> 
 
      <td align='center' width='5%'><input type='checkbox' name='first_name' /></td>\ 
 
      <td align='left'><?php echo $row_dept->dept; ?></td> 
 
     </tr> 
 
     <?php }; ?> 
 
     <tr> 
 
      <td colspan='2'><h3>Disposisi</h3><textarea id='disposisi' name='disposisi' class='form-control'></textarea></td> 
 
     </tr> 
 
     </table> 
 
     </form>`, 
 
    buttons: { 
 
    cancel: { 
 
     label: '<i class="fa fa-times"></i> Batal' 
 
    }, 
 
    confirm: { 
 
     label: '<i class="fa fa-check"></i> OK' 
 
    } 
 
    }, 
 
    callback: function(result) { 
 
    if (result == true) { 
 
     var disposisi = $("#test").find('textarea[name=disposisi]').val(); 
 
     alert(disposisi); 
 
    } 
 
    } 
 
});

+0

我試圖複製你的代碼並粘貼它。但是,它不起作用 – ashura91

+0

「不工作」是一個非常無用的評論。任何控制檯錯誤?查看頁面的來源以查看其他問題? – mplungjan

+0

沒有。只是沒有顯示任何其他的PHP代碼 – ashura91