当 url 包含 [...] 时如何发出警报
如果 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
扔掉那个片段。应该像
编辑
“$未定义”意味着您要么没有加载jQuery库,要么
$
被覆盖。我将整个片段放入一个自动执行的匿名函数中,这至少可以解决最后一个问题。
Throw away that snippet. Should be like
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.
url:contains
不是有效的 JavaScript,它会导致解析错误。与所有 jQuery 选择器一样,您实际上传递了一个字符串供 jQuery 解析:也就是说,
url:
不是有效的 jQuery 选择器。 jQuery 实际上并不是这个问题的解决方案。相反,使用window.location.href
获取当前 URL,并使用正则表达式对其进行测试:下一个问题是启用/禁用输入框的代码。它……已经破碎了。首先,如果元素有 ID,则只需使用
#id
选择器。其次,像:enabled
这样的选择器用于过滤元素 - 从 DOM 中提取更具体的元素来使用。他们实际上并没有改变他们选择的元素的状态。您想要这样的内容:
更新
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:That said,
url:
is not a valid jQuery selector. jQuery isn't actually the solution to this problem; instead usewindow.location.href
to get the current URL, and test it with a regular expression: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:
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.尝试这样的事情..
然后你可以做
try something like this..
and then you can do
尝试这个简单的事情。不确定这是否适合您。但它会找到单词“text”并禁用输入,
只需更改变量 searchTerm 并使用它即可。对我有用。
演示
Try this simple thing . Not sure this will suite you. But it will find for the word 'text' and disable the input
just change the variable searchTerm and use it. Works for me.
DEMO