我正在开发一个应用程序,他们使用 jquery 数据将变量传递给 html
当数据属性附加到 tr 标签时,它在站点的一个方面起作用。此代码有效
<tr class="js-instructions-row documents__table-row
<?=$ix.'row';?><?php //$ix==0 ? 'documents__table-row--active' : '' ?>"
data-product-title="<?= $sheet->name ?>"
data-instructions-image="<?= $serverpath.$thisImage ?>"
data-instructions-file="<?= $serverpath.'Instructions/'.$sheet->file ?>"
>
当我尝试将这些属性放在另一个 View 中的选择标签或选项标签上时,它没有通过。此代码不起作用。
<?php
foreach($instructions as $ix => $sheet) {
$thisImage = ($sheet->image?$sheet->image:'Image_holder_thumb.png');
?>
<option test="" data-product-title="<?= $sheet->name ?>" data-instructions-image="<?= Kohana::$config->load('aws.s3-baseurl-www-customercare').$thisImage ?>" data-instructions-file="<?= Kohana::$config->load('aws.s3-baseurl-www-customercare').'Instructions/'.$sheet->file ?>" value="<?=$sheet->id?>"><?=$sheet->name?></option>
<?php
}
?>
和冒犯 javascript:
$('.js-product-selector').on('change',function(e){
var selected = $(this).find('option:selected');
console.log(selected.attr('value'))
console.log(selected.data('product-title'));
$(".documents__product-title").text(selected.data('product-title'));
$(".documents__preview img").attr('src',selected.data('instructions-image'));
$(".documents__download").attr('href',selected.data('instructions-file'));
});
value 属性在日志中正常显示,但 data-product-title 没有 这是在 Controller 中调用我的 View 的方式。
$this->response->body(View::factory($this->folder."/instruction-sheets")->set('brands',ORM::factory('Brand')->with('Customercare_Instruction')->find_all())->set('postbrand',$brand));
有效的 View 嵌套在这样调用的 View 中:
$this->page=View::factory($this->folder.'/index');
$this->page->breadcrumb = 'Instruction Sheets';
$this->page->content = View::factory($this->folder."/instruction-sheets")->set('brands',ORM::factory('Brand')->with('Customercare_Instruction')->find_all());
subview 是这样调用的
<?= View::factory('customer-care/instruction-sheets-filtered')->set('instructions',$instructions)->render() ?>
非常感谢您的意见。
最佳答案
您绝不会用 Selectize 或 Select2 之类的东西来设置您的选择元素的样式,对吗?这可能就是从您的选项中剥离数据属性的原因。
关于javascript - JQuery 数据属性在 Kohana ajax 中被剥离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31709861/