2017-04-26 71 views
2

我創建了一個字段,選擇WordPress的後臺一highchart這樣的標籤:通行證從PHP中的文本字段值的JavaScript

public static function sm_register_chart() { 

    $charts = array(); 
    if(function_exists('wdt_get_all_charts_nonpaged')){ 
     foreach(wdt_get_all_charts_nonpaged() as $table){ 

      $charts[$table['id']] = $table['title']; 
     } 
    } 
    $captions = array(); 

    $fields = array(

     array(
      'label' => esc_html('Graphic'), 
      'description' => esc_html('Choose the graphic'), 
      'attr' => 'chart', 
      'type' => 'select', 
      'options' => $charts, 
     ), 
     array(
      'label' => esc_html('Footer label'), 
      'description' => esc_html('Choose the footer label'), 
      'attr' => 'footer_caption', 
      'type' => 'text', 
     ), 

    ); 

,我想通過無論是在footer_caption輸入到這個js我從這個小提琴了:http://jsfiddle.net/abenrob/ur02w4j5/

Highcharts.setOptions({ 
    chart: { 
      type: 'column', 
      events: { 
       load: function() { 
        var label = this.renderer.label("This text will adjust to chart resizing " + 
         "and redraws and will be visible on exported images.") 
        .css({ 
         width: '400px', 
         fontSize: '9px' 
        }) 
        .attr({ 
         'r': 2, 
         'padding': 5 
        }) 
        .add(); 

        label.align(Highcharts.extend(label.getBBox(), { 
         align: 'center', 
         x: 0, // offset 
         verticalAlign: 'bottom', 
         y: 0 // offset 
        }), null, 'spacingBox'); 

       } 
      }, 
      marginBottom: 120 
     }, 
     legend: { 
     align: 'center', 
     verticalAlign: 'bottom', 
     y: -30 
    }, 

這樣而不是var label = this.renderer.label("This text will adjust to chart resizing")我得到的footer_caption值。

我在想是這樣的:

var labelText = document.querySelectorAll('[footer_caption]').text() 
var label = this.renderer.label(labelText) 

但它不工作,即時通訊無法連得我從footer_caption

+0

是否'document.querySelectorAll( '[footer_caption]')'返回什麼?或者這是一個空陣列?無論如何,應該有:'document.querySelectorAll('[footer_caption]')[0] .innerText;'但我擔心'[footer_caption]'是一個錯誤的選擇器。如果你能夠展示生成的HTML元素的外觀,那麼我將能夠更多地瞭解它。 –

+0

看到我的編輯,我顯示完整的短代碼選項,其中我設置footer_caption – phpqs

+0

也許我應該以不同的方式問:在您的網站上生成「footer_caption」(因此您有一個標記,例如:'

')或它只是PHP數組中的一個元素? –

回答

0

鍵入值您可以通過執行實現這一目標以下;您可能必須根據您自己的代碼更改footer_caption的標識符;

<script type="text/javascript"> 

var footer_caption = <?= json_encode($arr['footer_caption']); ?>; 

</script> 

然後在JavaScript代碼中引用它所需的變量。使用json_encode確保非法字符等被正確處理。上面的代碼使用PHP快速標籤更多的參考可以找到here

+0

Ok im confused。我是否在js文件中有var footer_caption,然後執行: var label = this.renderer.label(footer_caption)? – phpqs

+0

是的,把這個'var footer_caption = <?= json_encode($ arr ['footer_caption']); ?>;'在JavaScript文件中與其他代碼一起使用,然後通過引用javascript變量'footer_caption'來使用該變量。您可能只需要更改'$ arr ['footer_caption']',以便它確實指向您自己代碼中的PHP變量。 – 2017-04-26 12:09:23

0

你可以使用json_encode做到這一點

<script> 
    var footer_caption = <?php echo json_encode($fields); ?>; 
</script> 
相關問題