2015-04-25 118 views
0

我想在没有页面刷新的情况下在div标签上显示PHP脚本的结果。 我的.c文件列表(用户可以编译它们在服务器上)...如何将值传递给JS函数,然后传递给PHP脚本

<option value="" selected="selected">Choose file </option>'; 

        $dirPath = dir('directoryExample'); 
        while (($file = $dirPath->read()) !== false) 
         { 
          if ($file == '.' || $file == '..') continue; 
          echo "<option value=\"" . trim($file) . "\">" . $file . "\n"; 
         } 
        $dirPath->close(); 
        echo '</select> 

...和两个股利和按钮这样的:

<div id="2" style="display:none;"> </div> 
<div id="1" style="display:visible;"> Compile...</div> 
<button type="button" onclick="codiad.ccomp.compile(this.value);">Compile</button> 

第一个问题:如何我可以将选定的文件传递给JQuery函数吗? this.value工作正常吗?

这是jQuery函数:

compile: function(value) { 

      if(($('#1').css('display')!='none')){ 
        $('#2').load(this.path+'\compile.php').show().siblings('div').hide(); 
      }else if($('#2').css('display')!='none'){ 
        $('#1').show().siblings('div').hide(); 
       } 

    } 

的代码工作正常,但是这是第二个问题:我怎么能现在都通过了价值PHP页面?

在此先感谢!

回答

2

您可以将数据参数传递给​​。

$('#2').load(this.path+'/compile.php', { file: $("#selectID").val() }) 
    .show().siblings('div').hide(); 

在PHP脚本中,使用$_GET['file']来获取选定的文件名。

0

我不认为在你的第一个问题this.value将正常工作,因为this指的是按钮,而不是select元素。

相反,你会需要这样的东西:

$('#your_select_element').val(); 

而且你可以删除内联JavaScript和使用这样的:

$('button').on('click', function() { 
    // do something with `$('#your_select_element').val();` 
    ... 
} 

假设有一个按钮,你可能需要添加一个类或ID来专门解决它。

而且在对方的回答中提到,您可以将数据参数添加到您的​​功能。