如何将值传递给 href 属性和目标

How to pass the value to the href attribute and target?

本文关键字:href 属性 目标 值传      更新时间:2023-09-26

我做了下面的jquery脚本。我试图在新选项卡中打开此网址,但这在IE浏览器上不起作用。如何将值传递给按钮,以便可以在新选项卡中打开?

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>

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

删除您提供的代码片段中的三个console.log('...')调用...那应该可以做到...

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

似乎您的目标是在单击名称="buscar"的图像时在新选项卡或新窗口中打开 url......

在这种情况下,我建议用这个替换 $.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);
                    }
                });