简体   繁体   English

在jquery选择器中调用php对象

[英]Calling a php object inside jquery selector

I am trying to access php variable inside jquery selector but it can't get to work. 我正在尝试访问jquery选择器中的php变量,但无法正常工作。 This php variable is taken from the views page from a foreach php statement. 该php变量来自foreach php语句的views页面。 Check the code below. 检查下面的代码。

HTML: HTML:

<?php foreach($items as $key => $value):?>
 <div id="uploader<?php $value['id'] ?>">Upload</div>
<?php endforeach?>

The above code works concat string. 上面的代码适用于concat字符串。

jQuery: jQuery的:

$(document).ready(function($) {
  $("#uploader<?php echo $value['id'] ?>").uploadFile({
  url:"YOUR_FILE_UPLOAD_URL",
  fileName:"myfile"
  });
});

Now I wanted to concat the php variable inside the jquery element selector but code above won't work. 现在,我想在jquery元素选择器中合并php变量,但是上面的代码将无法工作。 What would be the best thing to do here? 最好的办法是在这里做什么? Thanks 谢谢

You can also use ~ for selecting any id have the value as uploader. 您也可以使用~选择任何具有值的ID作为上载者。

$(document).ready(function($) {

    $('div[id~="uploader"]').uploadFile({
        url:"YOUR_FILE_UPLOAD_URL",
        fileName:"myfile"
    });
});

Ref: 参考:

[attribute^=value]  $("[title^='Tom']") All elements with a title attribute value starting with "Tom"
[attribute~=value]   $("[title~='hello']")  All elements with a title attribute value containing the specific word "hello"
[attribute*=value]  $("[title*='hello']")   All elements with a title attribute value containing the word "hello"

Try the following answer without php, select all the elements that have a id that starts with uploader 尝试以下不带php的答案,选择所有具有以uploader开头的id的元素

$(document).ready(function($) {
  $('div[id^="uploader"]').uploadFile({
  url:"YOUR_FILE_UPLOAD_URL",
  fileName:"myfile"
  });
});

or safer use a class 或更安全地使用课程

<?php foreach($items as $key => $value):?>
 <div class="toupload" id="uploader<?php $value['id'] ?>">Upload</div>
<?php endforeach?>

js: js:

$(document).ready(function($) {
      $('.toupload').uploadFile({
      url:"YOUR_FILE_UPLOAD_URL",
      fileName:"myfile"
      });
    });

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

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