将php变量传递给jQuery函数

时间:2016-10-22 10:17:31

标签: javascript php jquery

我搜索并尝试了几种不同的方法将php $变量传递给jQuery函数。我只需使用myfunction就可以传递一个字符串(" Hello");.但是,如果我尝试myfunction();或myfunction($ variable);有或没有引号它无法运行。

    <?php
        echo '<script>',
         'wholesection("Hello");',
         '</script>'
        ;
    ?>
</head>
<body>
    <?php
        $variable = "Hello";

        echo '<script>',
        'wholesection(' . $variable . ');',
        '</script>'
        ;
    ?>  

如果我使用:

发送用双引号括起来的文字字符串,则上述方法有效
'wholesection($variable);',
'wholesection("$variable");',

或其他类似的变体不起作用。

public void printReciept_non(int printer) {

    String reportSource = "receipt_non.jrxml"; //sale_report_new

    Map<String, Object> parameter = new HashMap<String, Object>();
    parameter.put("total_amount", priceWithDecimal(totalAmount));
    parameter.put("order_code", lbl_orderNo.getText());
    parameter.put("table_no", tableNo);
    parameter.put("IS_IGNORE_PAGINATION", true);

    Locale locale = new Locale("en", "US");
    parameter.put(JRParameter.REPORT_LOCALE, locale);

    try {
        JasperReport js = JasperCompileManager.compileReport(reportSource);
        JasperPrint jp = JasperFillManager.fillReport(js, parameter, new check().getConn());

        JasperPrintManager.printReport(jp, false);

        PrinterJob printerJob = PrinterJob.getPrinterJob();
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        int selectedService = printer;
        PrintService[] printService = PrintServiceLookup.lookupPrintServices(null, null);

        try {
            printerJob.setPrintService(printService[selectedService]);
        } catch (Exception e) {
            System.out.println(e);
        }

        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
        printRequestAttributeSet.add(new Copies(0));
        JRPrintServiceExporter exporter = new JRPrintServiceExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);

        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes());
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
        exporter.exportReport();

    } catch (Exception e) {
        System.out.println(e);
    }

}

3 个答案:

答案 0 :(得分:0)

您可以通过回显脚本中的php varriable

来传递它

尝试以下代码:

<?php
    $variable = "Hello";
?>
<script type="text/javascript">
    var simple = '<?php echo $variable; ?>';
</script>

答案 1 :(得分:0)

假设您的$variable有值"Hello"

然后这段代码:

echo 'wholesection('.$variable.');',

在html中重新格式化,如

wholesection(Hello);

请参阅?您已将Hello传递给某个功能。不是"Hello",而是HelloHello被视为javascript变量。我打赌你没有。

所以,修复是 - 添加引号:

echo 'wholesection("'.$variable.'");',

将呈现为:

wholesection("Hello");

答案 2 :(得分:0)

&#13;
&#13;
<?php
$variable = "Hello";

if(true){
 ?>
 <script>
  wholesection("<?php echo $variable?>");
 </script>
 <?
}
?>
&#13;
&#13;
&#13;