javascript - 单击不同的按钮时如何更改 div 内容.javascript php html

标签 javascript php jquery html css

<分区>


关闭 5 年前

我有三个按钮( Angular 色、部门、搜索)。单击 Angular 色时,div 将显示 Angular 色信息。 div代码是:

<div id="role_div" class="role" name="role" >
<?php 
...
$sql = "select distinct role from hpc_user ;"; 
echo '<table>';
$sel=$conn->query($sql);
while($row=$sel->fetch(PDO::FETCH_NUM))
{
  echo '<tr>';
  echo '<td style="word-break:break-all;">'.$row[0].'</td>';
  echo '</tr>';
}
  echo '</table>';
?>
</div>`

点击部门按钮时,我希望整个div内容都是

<div id="role_div" class="role" name="role" >
<?php 
$conn = new PDO('');
$conn->setAttribute(PDO::ATTR_ORACLE_NULLS, true);
$sql = "select distinct department from hpc_user ;"; 
echo '<table  >';
$sel=$conn->query($sql);
while($row=$sel->fetch(PDO::FETCH_NUM))
{
echo '<tr>';
echo '<td style="word-break:break-all;">'.$row[0].'</td>';
echo '</tr>';
}
?>
</div>

我尝试使用 innerhtml.document.getElementById('role_div').innerHTML='<?php>....</php>' , 但它不起作用。谁能帮助我?

最佳答案

您可以简单地将您的 Angular 色/部门数据放在两个隐藏的 div 中,然后您可以使用 Jquery 更改数据

您提到的另一个解决方案是您不能添加三个 div

HTML:

 <div id="ajax_exchange"></div>
 <input type="button" id="role" onclick="change_data(this.id);" value="role">
 <input type="button" id="department" onclick="change_data(this.id);" value="department">
 <input type="button" id="search" onclick="change_data(this.id);" value="search">

脚本:

<script type="text/javascript">
function change_data(id){
    var data_to_fetch = $('#'+id).val();

    $.ajax({
        type: "post",
        url: "<?php echo SFURI::SEFURL('index.php?itemtype=skus&layout=ajax_call',array('call'=>'ajax')); ?>",
        dataType: 'html',
        cache: false,    
        data: {get_this: data_to_fetch},
        success: function(data){
            $('#ajax_exchange').html("");
            $('#ajax_exchange').html(data);

        },
        error: function(){
            alert('Error while request..! try again');
        }
    });
}
</script>

ajax_call.php :

<?php
    if(isset($_POST['get_this']) && $_POST['get_this']=="role"){
        echo "role data";
        // here  you can put your role data to be sent to div
    }
    if(isset($_POST['get_this']) && $_POST['get_this']=="department"){

        echo "department data";
        // here  you can put your department data to be sent to div
    }
    if(isset($_POST['get_this']) && $_POST['get_this']=="search"){

        echo "search data";
        // here  you can put your search data to be sent to div
    }
?>

希望对您有所帮助。

关于javascript - 单击不同的按钮时如何更改 div 内容.javascript php html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45137384/

上一篇:javascript - 尽管在 html 中提供了正确的路径,但仍未正确找到图像源

下一篇:jquery - 当行分为两列时,Bootstrap 子菜单将宽度设置为 100%

相关文章:

javascript - 使用 window.open 将变量传递给新的 HTML

javascript - 面向对象的 D3 JS - 如何选择对象?

javascript - 如何使用 AudioContext 对象渲染静态 vaweform

javascript - scrollToElement 没有用户交互

jquery - 如何为我的下拉框激活 selected()

jquery - 使用 Jquery 进行多个过滤器

javascript - Firefox 不支持 JavaScript(表单验证),但 IE 完全支持

php - 在 codeigniter 路由中使用通配符?

php - MySQL查询缓存与应用层缓存结果集

php插入mysql数据库