2017-03-02 54 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