简体   繁体   English

此JQuery代码有什么问题?

[英]What is wrong with this JQuery code?

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">                                         

//waits till the document is ready
$(document).ready(function() {
    //onclick function
    $('button.butter').click(function(){
        //code will only run on certain url
        if (pathname.indexOf("google")>= 0){
            alert('This doesn't work anymore!');  

           //replacer
          $("a[href*='https://google.com']").attr('href',('http://www.google.com/search?q' + $('#FormID').val()));
        }
        else{
            alert('Wrong page');
        }
    });
});

</script>

It doesn't alert, but why? 它不会发出警报,但是为什么呢? This was working earlier and I forgot to save it. 这是较早的工作,我忘记保存了。 It should replace the url with another one plus the value from a field. 它应该用另一个URL加上字段的值来代替url。 edit: head, html, etc. tags are there. 编辑:头,html等标签在那里。

<button class="butter">Press</button>

You don't declare pathname anywhere, this: 您无需在任何地方声明pathname ,这是:

if (pathname.indexOf("google")>= 0){

should result in an error in your console. 应该会导致控制台错误。

我想你想要的是location.pathname而不是pathname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM