当 url 包含 [...] 时如何发出警报

发布于 2024-09-08 15:57:02 字数 1131 浏览 5 评论 0原文

如果 url 包含文本,如何使用 GM 禁用 jQuery 的输入字段?

我尝试过,

$(document).ready( function() {
    if($("url:contains('text')")){
        $(".input[id=field-owner]:disabled");
    }
    else {
        $(".input[id=field-owner]:enabled");
    }
});

但我不知道我做错了什么。而且我也不知道 url:code 是否是要使用的正则表达式。

更新

再次嗨, 感谢您花时间参与此内容。我想当 url 包含“newthread”时,询问/回答如何调用警报会更容易。

所以,我们有一个网址(例如): http://forum .jswelt.de/newthread.php?do=newthread&f=1 , 以及一个 id="subject" 的输​​入字段和一个“subject”标签。 如果 url 包含“newthread”,则应隐藏输入和标签元素。 以下代码有效,但不适合我。控制台什么也没说,也不会发出警报。


var url = window.location.href 
url = "http://forum.jswelt.de/newthread.php?do=newthread&f=1";
    var pos = url.search(/newthread/); 
    if(pos >= 0)
        alert("tadaa");
    else
        alert("mist");

:(

谢谢大家帮助我。

编辑插入最终解决方案


if (location.href.indexOf("text") != -1) {
$('#fieldname').attr("disabled", true); 

How can I disable an input-field with jQuery using GM if the url contains text?

I tried

$(document).ready( function() {
    if($("url:contains('text')")){
        $(".input[id=field-owner]:disabled");
    }
    else {
        $(".input[id=field-owner]:enabled");
    }
});

I don't know what I did wrong. And I also don't know whether url:code is a regular expression to use.

Update

Hi again,
thanks for your time putting in this. I guess it's easier to ask/answer how to call an alert when the url contains e.g. "newthread".

So, we have got a url(e.g.): http://forum.jswelt.de/newthread.php?do=newthread&f=1 ,
and an input-field with id="subject" and a label for "subject".
If the url contains "newthread" the input-and label-element should be hidden.
Following code works, but not for me. The console says nothing and won't alert though.


var url = window.location.href 
url = "http://forum.jswelt.de/newthread.php?do=newthread&f=1";
    var pos = url.search(/newthread/); 
    if(pos >= 0)
        alert("tadaa");
    else
        alert("mist");

:(

Thank you to all helping me.

Edit inserts the final solution


if (location.href.indexOf("text") != -1) {
$('#fieldname').attr("disabled", true); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小伙你站住 2024-09-15 15:57:03

扔掉那个片段。应该像

(function($){
  $(document).ready(function(){
     var url = window.location.href;

     if(/text/g.test(url)){
        $('#field-owner').attr('disabled', 'disabled');
     }
     else{
        $('#field-owner').removeAttr('disabled');
     }
  });
})(jQuery);

编辑

“$未定义”意味着您要么没有加载jQuery库,要么$被覆盖。
我将整个片段放入一个自动执行的匿名函数中,这至少可以解决最后一个问题。

Throw away that snippet. Should be like

(function($){
  $(document).ready(function(){
     var url = window.location.href;

     if(/text/g.test(url)){
        $('#field-owner').attr('disabled', 'disabled');
     }
     else{
        $('#field-owner').removeAttr('disabled');
     }
  });
})(jQuery);

edit

"$ not defined" means you either aren't loading the jQuery library or $ gets overwritten.
I put the whole snippet into a self-executing anonymous function which will fix the last issue at least.

隔岸观火 2024-09-15 15:57:03

url:contains 不是有效的 JavaScript,它会导致解析错误。与所有 jQuery 选择器一样,您实际上传递了一个字符串供 jQuery 解析:

$("url:contains('text')")

也就是说,url: 不是有效的 jQuery 选择器。 jQuery 实际上并不是这个问题的解决方案。相反,使用 window.location.href 获取当前 URL,并使用正则表达式对其进行测试:

if (window.location.href.match(/text/)) {
  // ...
}

下一个问题是启用/禁用输入框的代码。它……已经破碎了。首先,如果元素有 ID,则只需使用 #id 选择器。其次,像 :enabled 这样的选择器用于过滤元素 - 从 DOM 中提取更具体的元素来使用。他们实际上并没有改变他们选择的元素的状态。

您想要这样的内容:

if (window.location.href.match(/text/)) {
    // apply disabled attribute
    $('#field-owner').attr('disabled', 'disabled');
} else {
    // remove disabled attribute if it exists
    $('#field-owner').removeAttr('disabled');
}

更新

RE: $ not Defined:

您没有在页面中包含 jQuery 库。确保您已下载 jQuery 并通过

url:contains isn't valid JavaScript, and it will cause a parse error. As with all jQuery selectors, you're actually passing a string for jQuery to parse:

$("url:contains('text')")

That said, url: is not a valid jQuery selector. jQuery isn't actually the solution to this problem; instead use window.location.href to get the current URL, and test it with a regular expression:

if (window.location.href.match(/text/)) {
  // ...
}

The next problem is your code to enable/disable your input boxes. It's... beyond broken. Firstly, if the element has an ID, just use the #id selector. Secondly, selectors like :enabled are for filtering elements - pulling more specific elements from the DOM to work with. They don't actually change the state of the elements they select.

You want something like this:

if (window.location.href.match(/text/)) {
    // apply disabled attribute
    $('#field-owner').attr('disabled', 'disabled');
} else {
    // remove disabled attribute if it exists
    $('#field-owner').removeAttr('disabled');
}

Update

RE: $ not defined:

You aren't including the jQuery library in your page. Make sure you've downloaded jQuery and successfully included it in your page via a <script> tag.

一杆小烟枪 2024-09-15 15:57:03

尝试这样的事情..

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

然后你可以做

if (getUrlVars()["text"] != null) {
    $input.attr('disabled','disabled');
}

try something like this..

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

and then you can do

if (getUrlVars()["text"] != null) {
    $input.attr('disabled','disabled');
}
魂牵梦绕锁你心扉 2024-09-15 15:57:03

尝试这个简单的事情。不确定这是否适合您。但它会找到单词“text”并禁用输入,

​$('input').change(function(){
    var temp = $(this).val();
    var searchTerm = 'text';
    if(temp.search('text') > 0 || temp.search(searchTerm) == 0){
        $("#field-owner").attr('disabled', 'disabled');
    }
});​​​​​​

只需更改变量 searchTerm 并使用它即可。对我有用。
演示

Try this simple thing . Not sure this will suite you. But it will find for the word 'text' and disable the input

​$('input').change(function(){
    var temp = $(this).val();
    var searchTerm = 'text';
    if(temp.search('text') > 0 || temp.search(searchTerm) == 0){
        $("#field-owner").attr('disabled', 'disabled');
    }
});​​​​​​

just change the variable searchTerm and use it. Works for me.
DEMO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文