重新加载页面后打开新标签页

时间:2019-02-01 02:16:25

标签: jquery

我在打开新标签页时遇到问题。假设我在数据表上列出了一堆条形码,并且我必须选择需要扫描或创建pdf的条形码。选择后,我按下按钮以打开新选项卡(创建pdf)。但是我的问题是,它不是打开新选项卡,而是在当前页面上重新加载页面。这是我的代码

$('#btnscanbar').on('click', function() {
            var cekdata = $(this).attr('data-cek');
            var judul = $(this).attr('data-target');
            var target2 = $('#btnsearch').attr('data-target');
            var dataString = "judul=" + cekdata;        
            var fFactory = $("#fFactory").val();
            var fmachine = $("#fmachine").val();
            var fbrand = $("#fbrand").val();
            $.ajax
            ({
                type: "post",
                url:  host+ "AMS/ajax",
                data: dataString,
                dataType: 'json',
                cache: false,
                success: function(json_data)
                {
                    if(json_data.total==0)
                    {
                        swal("Please select barcode wanna scan", "warning");
                    }
                    else
                    {


                        window.open(url+'pdf?judul=' + judul + '&fFactory=' + fFactory +'&fmachine=' + fmachine +'&fbrand=' + fbrand, '_blank')                     
                        //reload current page
                        location.reload();
                    }
                }
            });
        });

任何解决方案??我需要打开新标签并重新加载页面,但不能使用该代码。

更新: 我找到了解决方案,我在打开新标签页之前正在使用警报,这里是我正在使用的代码

swal({ 
                            title: "Ready for Scan",
                            text: "Your Barcode succesed being scan, press OK to see",
                            type: "success",
                            confirmButtonText: "Ok",
                        }).then(function(){ 
                            window.open(url+'pdf?judul=' + judul + '&fFactory=' + fFactory +'&fmachine=' + fmachine +'&fbrand=' + fbrand, '_blank')                     
                            window.location.reload();
                        });         

2 个答案:

答案 0 :(得分:1)

这是我的解决方案。您可以参考。希望对您有帮助,我的朋友:))

<body>
    <form id="myForm">
      <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
        <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
      </div>
      <div class="form-group form-check">
        <input type="checkbox" class="form-check-input" id="exampleCheck1">
        <label class="form-check-label" for="exampleCheck1">Check me out</label>
      </div>
      <button onclick="myFunction()">Try it</button>
    </form>
 </body>   

<script>
    function myFunction() {
       //for open a new tab
       let win =  window.open("https://www.w3schools.com", "_blank");
       win.focus();
       //document.getElementById("myForm").reset();            
       location.reload(true); //Reloads the current page from the server 
    }   
</script>

答案 1 :(得分:0)

为此找到了解决方案

在这些代码上

 window.open(url+'pdf?judul=' + judul + '&fFactory=' + fFactory +'&fmachine=' + fmachine +'&fbrand=' + fbrand, '_blank')                     
                    //reload current page
                    location.reload();

我变成了

swal({ 
                            title: "Ready for Scan",
                            text: "Your Barcode succesed being scan, press OK to see",
                            type: "success",
                            confirmButtonText: "Ok",
                        }).then(function(){ 
                            window.open(url+'pdf?judul=' + judul + '&fFactory=' + fFactory +'&fmachine=' + fmachine +'&fbrand=' + fbrand, '_blank')                     
                            window.location.reload();
                        });    

它运行良好,希望有人遇到像我这样的问题,请使用这些代码。 :)