简体   繁体   English

将变量从php传递到javascript以用作ID

[英]Passing variable from php to javascript for use as an ID

I am attempting to build a small application where our editors can choose to hide some elements, in this case, Job Names, from a page when it is printed. 我正在尝试构建一个小应用程序,我们的编辑可以选择在打印时从页面中隐藏一些元素,在本例中为Job Names。

However, I cannot seem to get the addclass to work properly. 但是,我似乎无法让addclass正常工作。 It either does nothing or it adds the class to every checkbox. 它要么什么也不做,或者它将类添加到每个复选框。

To give my checkboxes a unique id, I am using the job name as the ID in php. 为了给我的复选框一个唯一的ID,我使用作业名称作为PHP中的ID。 These come from a database. 这些来自数据库。 Input: 输入:

<input type='checkbox' id='".$row['name']"'/><div class='tohide ".$row['name']."'>".$row['name']."</div>

Javascript: 使用Javascript:

var src = <?php echo json_encode($row['name']); ?>;
  $('input#src').change(function(){
    if(this.checked){
      $('.tohide').addClass('bold noprint');
    }else{
      $('.tohide').removeClass('bold noprint');
    }
  })

I have noticed that <?php echo json_encode($row['name']); ?> 我注意到<?php echo json_encode($row['name']); ?> <?php echo json_encode($row['name']); ?> and <?php echo $row['name']; ?> <?php echo json_encode($row['name']); ?><?php echo $row['name']; ?> <?php echo $row['name']; ?> do not give any results. <?php echo $row['name']; ?>不给任何结果。 I am using the same $row['name'] inside the php to generate proper ID'd for the checkboxes. 我在php中使用相同的$row['name']来为复选框生成正确的ID。

Client Side code: 客户端代码:

<script type="text/javascript">
      var src = "";
      $('input#'+ src).change(function(){
        if(this.checked){
          $('.tohide').addClass('bold noprint');
        }
        else {
          $('.tohide').removeClass('bold noprint');
        }
      })
    </script>

When using json_encode I do get a value of null 使用json_encode我得到一个null值

This is just a string: 这只是一个字符串:

'input#src'

It's not going to interpolate a variable name within the string, it's just a literal string. 它不会在字符串中插入变量名,它只是一个文字字符串。 To use the src variable, concatenate it: 要使用src变量,请将其连接起来:

'input#' + src

I also suspect that this may be incorrect: 我也怀疑这可能是不正确的:

var src = <?php echo json_encode($row['name']); ?>;

Though you really should look at the actual client-side code to confirm. 虽然你真的应该看看实际的客户端代码来确认。 But if the value is simply a string then I doubt you need json_encode . 但如果值只是一个字符串,那么我怀疑你需要json_encode But you would definitely need quotes around a string: 但你肯定需要一个字符串的引号

var src = '<?php echo $row['name']; ?>';

The problem why all checboxes change is this: 所有checbox改变的原因是:

$('.tohide').removeClass('bold noprint');

All your checkboxes match the expression ".tohide". 您的所有复选框都与“.tohide”表达式匹配。

You'll want to do $('.tohide.'+this.id).removeClass('bold noprint') instead. 你想要做$('.tohide.'+this.id).removeClass('bold noprint')

Disclaimer: not tested since you did not provide valid HTML and this is a HTML / JS question. 免责声明:未经测试,因为您没有提供有效的HTML,这是一个HTML / JS问题。 The PHP part is irrelevant. PHP部分无关紧要。

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

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