我试图访问jquery选择器中的php变量,但它无法工作。这个php变量是从foreach php语句的views页面中提取的。检查下面的代码。
HTML:
<?php foreach($items as $key => $value):?>
<div id="uploader<?php $value['id'] ?>">Upload</div>
<?php endforeach?>上面的代码可以连接字符串。
jQuery:
$(document).ready(function($) {
$("#uploader<?php echo $value['id'] ?>").uploadFile({
url:"YOUR_FILE_UPLOAD_URL",
fileName:"myfile"
});
});现在,我想在jquery元素选择器中连接php变量,但是上面的代码不能工作。在这里最好做什么?谢谢
发布于 2016-05-05 03:56:15
在不使用php的情况下尝试下面的答案,选择所有元素,这些元素的id都以uploader开头
$(document).ready(function($) {
$('div[id^="uploader"]').uploadFile({
url:"YOUR_FILE_UPLOAD_URL",
fileName:"myfile"
});
});或者更安全地使用类
<?php foreach($items as $key => $value):?>
<div class="toupload" id="uploader<?php $value['id'] ?>">Upload</div>
<?php endforeach?>联署材料:
$(document).ready(function($) {
$('.toupload').uploadFile({
url:"YOUR_FILE_UPLOAD_URL",
fileName:"myfile"
});
});发布于 2016-05-05 03:53:46
您还可以使用~来选择任何具有该值的id作为上传器。
$(document).ready(function($) {
$('div[id~="uploader"]').uploadFile({
url:"YOUR_FILE_UPLOAD_URL",
fileName:"myfile"
});
});参考文献:
[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"https://stackoverflow.com/questions/37042023
复制相似问题