javascript - JQuery 数据属性在 Kohana ajax 中被剥离

标签 javascript jquery html ajax kohana

我正在开发一个应用程序,他们使用 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/

相关文章:

javascript - 如何在 div 前面添加当前年份?

javascript - 使用 shift() 和 push() 循环数组值与使用计数器变量相比,最佳方法是什么?

html - 当高度设置为 100% 时,为什么字段集会超过其容器的高度?

javascript - 在 AngularJS 中调用 ajax 后执行服务

javascript - aws-amplify-react 和 @aws-amplify/ui-react 有什么区别?

javascript - lodash debounce 在匿名函数中不起作用

javascript - 卡片翻转动画 Internet Explorer 11

javascript - 在 JS 的循环中返回一个随机数,但从不相同

html - 在 bootstrap 3 中缩小我的导航栏图像的中间

javascript - 代码编辑器响应窗口