简体   繁体   English

根据php变量的值使用Java隐藏div

[英]Hiding div with Javascript based on value of php variable

I currently have a select box in my laravel blade: 我的Laravel刀片目前有一个选择框:

<div style="text-align:center;">
   <div>
    <span style="color:#fff;"><strong>Sort by:</strong></span>
    <select id="filterText" class="uk-text-muted" style="margin-top:10px; width:33%; height:30px; font-size: 16px;" onchange="filterText()" >
      <option id="allitems" class="uk-text-muted" style="font-size: 16px;" selected data-default value="" selected data-default>All Items</option>
      <option id="popular" class="uk-text-muted" style="font-size: 16px;" value="" >Popularity</option>
      <option id="recent" class="uk-text-muted" style="font-size: 16px;" value="">Recently Ordered </option>
     </select>
   </div>

And I want to use JS to show/hide my .groupcontainer div based on the value of a PHP variable. 我想使用JS基于PHP变量的值显示/隐藏我的.groupcontainer div。 I have a function that creates PHP variables $topsellers and $reorder . 我有一个创建PHP变量$topsellers$reorder的函数。 So I want to map my option popularity to $topsellers and the option Recently Ordered to $reorder 因此,我想将我的期权popularity映射到$topsellers并将“ Recently Ordered的期权”映射到$reorder

I have some JS that I was playing with but I don't know how to do this properly, however, it gives an idea: 我有一些正在使用的JS,但我不知道如何正确执行此操作,但是它给出了一个主意:

<script type="text/javascript">
$('#filterText').on('change', function () {
if (this.value == '') {
  $(".groupcontainer").show();
} else {
  $(".groupcontainer").hide();
}
});
</script>

But basically, I want to say : 但基本上,我想说:

if value popularity is selected and $topsellers == true , show the groupcontainer div, and if value recently ordered is selected and $reorder is true, only show those groupcontainer divs. 如果选择了值流行度并且$topsellers == true ,则显示$topsellers == true div,如果选择了最近订购的值并且$reorder为true,则仅显示那些groupcontainer div。

I hope that makes sense. 我希望这是有道理的。 I feel like the pieces are there but I need help connecting them with logic and syntax. 我觉得这些东西都在那里,但我需要帮助将它们与逻辑和语法联系起来。

UPDATE (new attempted solution): UPDATE(新的尝试解决方案):

JS: JS:

<script type="text/javascript">

$('#filterText').on('change', function () {

var topsellers = "<?php echo $pgroups->topseller; ?>";
var reorder    = "<?php echo $pgroups->reorder; ?>";

 var currentVal = $(this).val();

 if (currentVal == 'popularity' && topsellers == "true" || currentVal == 'recently_ordered' && reorder == "true") {
   $(".groupcontainer").show();
 } else {
   $(".groupcontainer").hide();
 }
});
</script>

HTML/Select box: HTML /选择框:

    <div style="text-align:center;">
                                <div>
                                    <span style="color:#fff;"><strong>Sort by:</strong></span>
                                    <select id="filterText" class="uk-text-muted" style="margin-top:10px; width:33%; height:30px; font-size: 16px;" onchange="filterText()" >
                                        <option id="allitems" class="uk-text-muted" style="font-size: 16px;" selected data-default value="" selected data-default>All Items</option>
                                      <option id="popular" class="uk-text-muted" style="font-size: 16px;" value="popularity" >Popularity</option>
                                      <option id="recent" class="uk-text-muted" style="font-size: 16px;" value="recently_ordered">Recently Ordered </option>
                                    </select>
                                </div>
                            </div>

You could add hidden inputs for those values like : 您可以为以下值添加隐藏的输入:

<input class="topsellers" value="{{$topsellers}}" />
<input class="reorder" value="{{$reorder}}" />

Or you could put the variables to the Js variables like : 或者您可以将变量放入Js变量中,例如:

var topsellers = "<?php echo $topsellers; ?>";
var reorder    = "<?php echo $reorder; ?>";

Then use those value in the condition in your JS code, something like : 然后在JS代码的条件中使用这些值,例如:

$(function(){
    var topsellers = "<?php echo $topsellers; ?>";
    var reorder    = "<?php echo $reorder; ?>";

    $('#filterText').on('change', function() {
      var currentVal = $(this).val();

      if (currentVal == 'popularity' && topsellers == "1" || currentVal == 'recently_ordered' && reorder == "1") {
        $(".groupcontainer").show();
      } else {
        $(".groupcontainer").hide();
      }
    });
});

Hope this helps. 希望这可以帮助。

Since PHP is a backend language you have to expose the variable in javascript context which is at the browser level. 由于PHP是一种后端语言,因此您必须在浏览器级别的javascript上下文中公开该变量。 To do so what you would end up doing is something like this: 为此,您最终将要做的事情是这样的:

<script type="text/javascript">
var somePHPVariable = "<?php echo $SOMEVAR; ?>";
...
</script>

That now gives you the PHP variable at the javascript level so you can do operations on it just like you would with any other javascript variable. 现在,您可以在javascript级别获得PHP变量,因此可以像对其他任何javascript变量一样对它进行操作。 Just keep in mind that PHP renders / computes before javascript does, so your php variables will always be set first. 请记住,PHP在javascript之前先渲染/计算,因此您的php变量将始终被首先设置。 The alternative to this would be to "ask php" via rest / ajax request what the value is and then continue calculation based on the result. 替代方法是通过rest / ajax请求“询问php”值是什么,然后根据结果继续计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM