【问题标题】:Show/Hide div id from input value form previous page从上一页的输入值显示/隐藏 div id
【发布时间】:2018-07-29 00:03:29
【问题描述】:

我觉得很难解决这个问题。我想使用上一页的表单输入数据数据来显示或隐藏特定的 div id。

<form action="http://example.com/example/" method="post">
                <input type="hidden" id="example" name="example" value="<?php echo $site; ?>">
          <input class="examplesubmit" type="submit" value="Submit">
</form>

第 2 页

<div id="testing"  style="display:none;">
content
</div>

我希望将上一页的输入值或名称或 id 用作 div id 显示或隐藏的基础。如果用户点击提交按钮,页面自动显示内容,否则不显示内容

我应该怎么写?可以使用 javascript 或 php 代码

【问题讨论】:

  • 到目前为止,您是否编写过任何服务器端代码来尝试解决此问题?
  • 我会把它放在url的参数中,比如site.xz/form/page2/?whatever=yes

标签: javascript php html show hidden


【解决方案1】:

您可以通过以下方式实现:

第 1 页

<form action="index2.php" method="post">
    <input type="hidden" id="example" name="example" value="foo">
    <input class="examplesubmit" type="submit" value="Submit">
</form>

第 2 页 (index2.php)

<div id="testing"<?php echo ($_POST['example'] == 'foo') ? '' : ' style="display:none;"'; ?>>
    konten
</div>

这利用了ternary operator,它或多或少是一个内联 if/else 条件。

因此,在我的示例中,第 1 页上的表单提交到第 2 页上的表单 (index2.php) 并检查名称为 example 的表单字段的值是否设置为“foo”。

【讨论】:

  • 值是动态的,我想使用动态值。怎么做?
【解决方案2】:

您只能在同一页面中包含两者,默认隐藏内容,仅显示单击提交按钮。

<form action="http://example.com/example/" method="post">
                <input type="hidden" id="example" name="example" value="<?php echo $site; ?>">
          <input class="examplesubmit" type="submit" value="Submit">
</form>

这里你有第二页,只有在这里使用第一页

<?php require_once 'page2.php'; ?>

这里是 jquery 或 javascript,抱歉我更喜欢 jquery。

<script>
$(function(){
 $("#testing").hide();
   $('.examplesubmit').click(function(e){
     e.preventDefault();
     $("#testing").show();
});
});
</script>

【讨论】:

  • 我试过了,但它不起作用。我使用 wordpress 平台 .. 在这种情况下 page.php 到单个 php。我希望在用户单击提交按钮时显示 div,如果没有,则不显示内容。我尝试使用 WebCode.ie 用户建议的 foo 方法。是的,如果值是固定的,它可以工作,但我在应用时使用动态值不起作用。这个问题还有 3 天让我很沮丧:(
猜你喜欢
  • 2019-06-17
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多