【问题标题】:How to pass the value to the href attribute and target?如何将值传递给 href 属性和目标?
【发布时间】:2016-06-16 16:46:09
【问题描述】:

我做了以下 jquery 脚本。我试图在新选项卡中打开此 url,但这不适用于 Internet Explorer。如何将值传递给按钮,以便在新选项卡中打开?

var checkSurl = "<%=checkSURL%>";
        var iframeDetalleSOnLoad = function(obj) {
            $(obj).show();
        };

        $(document).ready(function() {

            $("#detalleS").html("");

            $("[name=buscar]").on("click", function() {
                var bottonBuscar = $(this);


                var numS = $("#numS");
                if (numS.val() == "") {
                    alert("El numero de s no puede estar vacestar vacío");
                }
                else {
                    // Call per verifica existence of s
                    $.ajax({
                        url: checkSurl + "?numS=" + $("#numS").val(),
                        success: function(response) {
                            var isS = eval(response.trim());
                            console.log(isS);

                            // returns che il s esiste
                            if (isS) {
                                var targetURL = "https://www.sample.es/cgi-bin/fccgi.exe?w3exec=DET&CONTROL=samole&ID=" + numS.val();
                                bottonBuscar.parent('a').attr('href',targetURL);
                                bottonBuscar.parent('a').attr('target', '_blank');
                                console.log(bottonBuscar.parent('a').attr('href',targetURL));
                                console.log(bottonBuscar.parent('a').attr('target', '_blank'));

                            }
                            else {
                                alert("EL NUMERO DE S QUE HA INSERTADO NO ES CORRECTO.");
                                return false;
                            }
                        },
                        error: function(error) {
                            $("#detalleS").html(error);
                        }
                    });
                }

我想要更改的 html 元素是 href 并且可能将目标设置为 '_blank'。

<div class="formulario" >
                        <div align="center">
                            <a href="#"
                               onMouseOut="MM_swapImgRestore()"
                               onMouseOver="MM_swapImage('buscar','','<%=request.getContextPath()%>/img/buttonBuscarEnvio_over.png',1)">
                                <img src="<%=request.getContextPath()%>/img/buttonBuscarEnvio.png" alt="Buscar S"
                                     name="buscar" border="0" id="buscar"></a>
                        </div>

【问题讨论】:

    标签: javascript jquery html internet-explorer cross-browser


    【解决方案1】:

    这里的主要问题是您已包含console.log() 函数,Internet Explorer 将抛出错误,因此后续脚本在console.log() 调用后将无法运行。所以你可以尝试删除它们...

    删除您提供的代码 sn-p 中的三个 console.log('...') 调用...应该这样做...

    或者你可以像window.console &amp;&amp; window.console.log('text')一样使用它

    看来您的目标是在单击名称为“buscar”的图像时在新选项卡或新窗口中打开网址...

    在这种情况下,我建议用这个替换 $.ajax 块

                       $.ajax({
                        url: checkSurl + "?numS=" + $("#numS").val(),
                        success: function(response) {
                            var isS = eval(response.trim());
    
                            // returns che il s esiste
                            if (isS) {
                                var targetURL = "https://www.sample.es/cgi-bin/fccgi.exe?w3exec=DET&CONTROL=samole&ID=" + numS.val();
                                window.open(targetURL, "_blank");
    
                            }
                            else {
                                alert("EL NUMERO DE S QUE HA INSERTADO NO ES CORRECTO.");
                                return false;
                            }
                        },
                        error: function(error) {
                            $("#detalleS").html(error);
                        }
                    });
    

    【讨论】:

    • 谢谢。已经解决而不是 window.open(targetURL, "_blank");我写道:oNewWindow = window.open(targetURL); console.log() 不是问题
    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 2017-08-16
    • 2012-11-10
    • 2020-05-17
    • 2017-03-02
    • 1970-01-01
    • 2015-06-29
    • 2017-04-15
    相关资源
    最近更新 更多