我不知道在moodle中将php的id值传递给jquery。有一个两个文件索引.php和 myscript.js
索引.php文件:
html_writer::tag('button','', array('id' => 'buttonCompile', 'data-url' => $urlbase));
我的脚本.js文件:
$(document).ready(function(){
alert($('#button').attr('id'));
});
我需要在JS文件中显示"数据URL"的值
PD:html_writer是用于编写HTML代码的Moodle函数,在本例中是一个按钮标签。
因为按钮的ID是buttonCompile
,您将无法找到任何ID button
的项目。您可以使用以下代码在 jQuery 中获取 buttonCompile
属性的数据 url:
$(document).ready(function(){
alert($('#buttonCompile').data('url'));
//or $('#buttonCompile').attr("data-url")
});
CFR.如何获取数据 id 属性?
但是,如果您想获取按钮的 ID(并且页面上只有一个按钮。你可以这样做:
$(document).ready(function(){
alert($('button').attr('id'));
});