在web共享api javascript函数中传递复制的foreach值

在web共享api javascript函数中传递复制的foreach值,javascript,jquery,laravel,laravel-7,web-share,Javascript,Jquery,Laravel,Laravel 7,Web Share,我试图分享一个特定产品的链接。 在foreach循环中,我调用用户的产品,以便他们可以通过按share来共享它。API正在运行 但是,每个按钮通过第一个id都有重复,我没有任何解决方案。。。 从两天开始尝试不同的事情,但都是心血来潮。。。 请引导我找到解决方案谢谢 这是按钮代码 @foreach($sellers as $reports) <input id="myInput" type="button" data-name="{

我试图分享一个特定产品的链接。 在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
@foreach($sellers作为$reports)
@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
@push('myjs')
函数addRow(ele)
{
变量名称=$(ele.attr('data-name');
变量id=$(ele.attr('data-id');
变量文本=”http://127.0.0.1:8000/scan-s报告/“+id+”/登录页”;
var subject=“报告链接用于”+名称;
控制台日志(主题);
$(文档)。在('单击',()=>{
如果(navigator.share!==未定义){
navigator.share({
//console.log('imif');
文本:文本,
//标题:“Web共享API草稿”,
//网址:'https://wicg.github.io/web-share/#share-方法',
})
。然后(()=>console.log('Successful share'))
.catch((错误)=>console.log('error sharing',error));
}
//否则{
////console.log('I m else');
//window.location='mailto:?subject=用于+name+和body的报告链接=http://127.0.0.1:8000/scan-sm报告/+id+/登录页';
// }
});
}
@端推
@阿努拉德

如果您使用onclick=“addRow()”那么我认为您不需要('click',…)上的$()。他们在做同样的事情。我想你可以把里面的代码移出()=>{…}祝你好运 @阿努拉德 这对我来说很管用,就像一个符咒: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>
@endpush
@push('myjs')
函数addRow(ele)
{
变量名称=$(ele.attr('data-name');
变量id=$(ele.attr('data-id');
变量文本=”http://127.0.0.1:8000/scan-s报告/“+id+”/登录页”;
var subject=“报告链接用于”+名称;
控制台日志(主题);
//$('input[type=button]')。在('click',()=>{
如果(navigator.share!==未定义){
navigator.share({
//console.log('imif');
文本:文本,
//标题:“Web共享API草稿”,
//网址:'https://wicg.github.io/web-share/#share-方法',
})
。然后(()=>console.log('Successful share'))
.catch((错误)=>console.log('error sharing',error));
}
//否则{
////console.log('I m else');
//window.location='mailto:?subject=用于+name+和body的报告链接=http://127.0.0.1:8000/scan-sm报告/+id+/登录页';
// }
// });
}
@端推

是否检查按钮生成的html是否具有唯一的
数据id
?还有,
id
有什么?是的,他们有唯一的id,id有数据库中一行的id。有些事情我觉得很奇怪:你为每个输入设置了相同的id,然后点击整个文档进行注册。通常,您会在点击按钮时注册,如$(输入[type=button])。点击('click')。然后,您可以通过$(this.data('name')在函数中获取数据。如果使用onclick=“addRow()”,那么我认为您不需要$()。on('click',…)。他们在做同样的事情。我想你可以把里面的代码移出()=>{…}祝你好运。很高兴我能帮上忙!