我正在尝试分享特定产品的链接。在foreach循环中,我调用用户的产品,这样他们就可以通过按share来共享它。API正在工作。
但是,每个按钮传递的第一个id都是重复的,我没有任何解决方案……从两天以来尝试了不同的东西,但都是在静脉……请引导我找到解决方案,谢谢....
这是按钮代码
@foreach($sellers as $reports)
<input id="myInput" type="button" data-name="{{$reports['title']}}" data-id="{{$reports['id']}}" value="{{$reports['id']}}" onclick="addRow(this)">
@endforeach下面是js函数
@push('myjs')
<script>
function addRow(ele)
{
var name= $(ele).attr('data-name');
var id= $(ele).attr('data-id');
var text = "http://127.0.0.1:8000/scan-s-report/"+ id +"/landing-page";
var subject = "Report link for"+ name ;
console.log(subject);
$(document).on('click', () => {
if (navigator.share !== undefined) {
navigator.share({
// console.log('I m if');
text: text,
// title: 'Web Share API Draft',
// url: 'https://wicg.github.io/web-share/#share-method',
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing', error));
}
// else {
// // console.log('I m else');
// window.location = 'mailto:?subject=Report link for + name +&body=http://127.0.0.1:8000/scan-sm-report/+ id +/landing-page';
// }
});
}
</script>
@endpush@Anurad

发布于 2020-10-16 02:09:30
如果你使用点击“addRow()”,那么我认为你不需要$().on(‘onclick=’,...)。他们也在做同样的事情。我想你可以把代码移出() => {...}祝你好运@Anurad这对我来说很管用,它就像一个咒语:D
@push('myjs')
<script>
function addRow(ele)
{
var name= $(ele).attr('data-name');
var id= $(ele).attr('data-id');
var text = "http://127.0.0.1:8000/scan-s-report/"+ id +"/landing-page";
var subject = "Report link for"+ name ;
console.log(subject);
// $('input[type=button]').on('click', () => {
if (navigator.share !== undefined) {
navigator.share({
// console.log('I m if');
text: text,
// title: 'Web Share API Draft',
// url: 'https://wicg.github.io/web-share/#share-method',
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing', error));
}
// else {
// // console.log('I m else');
// window.location = 'mailto:?subject=Report link for + name +&body=http://127.0.0.1:8000/scan-sm-report/+ id +/landing-page';
// }
// });
}
</script>
@endpushhttps://stackoverflow.com/questions/64376828
复制相似问题