2017-01-23 41 views
1

我的问题是在IM的Prestashop工作(1.6.1.9)CMS网页,即时通讯不许放任何脚本到CMS的网页源代码。(我不是使用iframe中对于这一点,它不是一个解决方案)..的Javascript链接<select> ID

所以我有一个下拉菜单,其中每个链接上的下拉链接到某个页面。我想用一个java脚本功能来做到这一点。

但因为我不能这样调用示例中的页面上的JavaScript,

<select id="selectdrops" name="newurl" onchange="menu_goto(this.form) "> 
    <option value="/index.html">Home</option> 
    <option value="/feedback.html">Feedback</option> 
    <option value="/links.html">Related Links</option> 
</select> 

我愿我的javascript重点下拉“ID”如果多数民众赞成可能吗?

删除:menu_goto(this.form)我想仍然可以完成有针对性的链接,可能只关注选择“ID”。

我的.js文件看起来是这样的:

$(document).ready(function() { 

    $(document).on('change', '#dropSelectAbout', function (e) { 
     e.preventDefault(); 
     menu_goto(); 
    }); 

    $(document).ready(function() { 
     $("a").click(function(event) { 
      alert(event.target.id); 
     }); 
    }); 

}); 

function menu_goto(menuform) 
{ 
    var baseurl = window.document.location.protocol + '//' + window.document.location.host; 
    selecteditem = menuform.newurl.selectedIndex ; 
    newurl = menuform.newurl.options[ selecteditem ].value ; 
    if (newurl.length != 0) { 
    location.href = baseurl + newurl ; 
    } 
} 

回答

1

我建议进行以下更改;

$(document).on('change', '#selectdrops', function (e) { 
     e.preventDefault(); 
     menu_goto(e.target.value); 
    }); 
}) 

function menu_goto(newurl) { 
    var baseurl = window.document.location.protocol + '//' + window.document.location.host; 

    if (newurl.length != 0) { 
     window.location = baseurl + newurl ; 
    } 
} 

删除:(删除下面的脚本,因为它的目标是所有的< a>标签它创建了一个弹出的对话框中的消息时,链接被点击,在顶部的功能只是专注于我的下拉需要的脚本,其中id =“selectdrops”)

$(document).ready(function() { 
     $("a").click(function(event) { 
      alert(event.target.id); 
     }); 
    }); 

e.target.value会给你所选择的页面上选项,因此您可以将该值直接传递到您的menu_goto函数中。
location没有因此一个ref财产使用window.location如有意向重定向到新建成的URL。

+0

你是真正的英雄,工作,感谢! – Ylama

0

如果你想上的变化(点击按钮后没有)重定向你可以使用这个脚本:

$(document).ready(function() { 

    $(document).on('change', '#selectdrops', function (e) { 
      e.preventDefault(); 
      menu_goto(this); 
     }); 

}); 

function menu_goto(menuform) 
{ 
    if(menuform == null) 
     return; 

    newurl = $(menuform).val() ; 

    if (newurl.length != 0) { 
     location.href = baseUri + newurl ; 
    } 
} 

您可以使用基本URI Prestashop用来获取商店网址的变量。这对子域更有效。 然后在没有/(它已经在baseUri中)的情况下在select中设置链接。

编辑: 要使用按钮点击:

$(document).ready(function() { 

    $(document).on('click', '#golink', function (e) { 
      e.preventDefault(); 
      menu_goto(); 
     }); 

}); 

function menu_goto() 
{ 
    var menuform = $('#selectdrops'); 

    if(menuform == null) 
     return; 

    newurl = menuform.val() ; 

    if (newurl.length != 0) { 
     location.href = baseUri + newurl ; 
    } 
} 

在这种情况下,该按钮链接到执行必须有ID golink。

+0

非常感谢那,但是我想点击一个选项,然后它应该重定向到相应的页面,在Value属性中。也许我应该给

0

你忘了通过形式为menu_goto函数(也选择是假的参数在此被校正的代码:

$(document).ready(function() { 
 

 
    $(document).on('change', '#selectdrops', function(e) { 
 
    e.preventDefault(); 
 
    menu_goto(this.form); 
 
    }); 
 

 
    $(document).ready(function() { 
 
    $("a").click(function(event) { 
 
     alert(event.target.id); 
 
    }); 
 
    }); 
 

 
}); 
 

 
function menu_goto(menuform) { 
 
    var baseurl = window.document.location.protocol + '//' + window.document.location.host; 
 
    selecteditem = menuform.newurl.selectedIndex; 
 
    newurl = menuform.newurl.options[selecteditem].value; 
 
    if (newurl.length != 0) { 
 
    location.href = baseurl + newurl; 
 
    } 
 
}
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script> 
 

 
<form> 
 
    <select id="selectdrops" name="newurl"> 
 
    <option value="/index.html">Home</option> 
 
    <option value="/feedback.html">Feedback</option> 
 
    <option value="/links.html">Related Links</option> 
 
    </select> 
 
</form>

+0

谢谢你,我试过这个,当菜单下降,我选择

+0

在上下文之外尝试此解决方案可以正常工作,事件被触发并且页面发生更改(如上例)。这可能是由于prestashop中的其他事情。也许所有这些都在iframe中,并且您正在更改iframe的位置。也许prestashop不会让你改变位置。您应该在位置更改线上的检查器中放置一个断点,您可以确定。 –