简体   繁体   English

jQuery多重选择器

[英]Jquery multiple selector

I am trying to select one input box, But because i may have more than one inputs, i am trying to limit the one i am selecting. 我试图选择一个输入框,但是因为我可能有多个输入,所以我试图限制我选择的输入框。 Here is my jquery code that select the the input but for some reason it doesn't work. 这是我的jquery代码,用于选择输入,但是由于某种原因它不起作用。 Is theresomething i am doing wrong? 我在做错什么吗? This is be executed once the document is ready. 文档准备就绪后将执行此操作。

if ( $("input[name='RETURN.URL'][value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']").length > 0 )
{
    alert("Transcript report");
}
else
{
    alert("Not Transcript Report");
}

Here the html 这里的HTML

<input type="hidden" name="RETURN.URL" value="http://link.com?TYPE=P&amp;PID=ST-L09F166&amp;CONSTITUENCY=WBST">
<input type="hidden" name="SUBMIT_OPTIONS" value="">

Your selector is using the HTML escaped attribute value: 您的选择器正在使用HTML转义属性值:

[value='http://link.com?TYPE=P&amp;PID=ST-L09F166&amp;CONSTITUENCY=WBST']

The actual value of the selector should use & and not &amp; 选择器的实际值应使用&而不是&amp; :

[value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']

The value was HTML escaped so that the value would be correctly represented within the attribute, but the query selector needs to match by the actual value. 该值是经过HTML转义的,以便可以在属性中正确表示该值,但是查询选择器需要按实际值进行匹配。

You'd probably be better off using a simple selector such as a class. 使用简单的选择器(例如类)可能会更好。 This is particularly important, as the URL represented in the [value] attribute could have its query string parameters in any order and represent the same resource. 这一点特别重要,因为[value]属性中表示的URL可以具有任意顺序的查询字符串参数,并表示相同的资源。

use jQuery attr() method 使用jQuery attr()方法

if($("input[value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']").attr('name') == 'RETURN.URL'){
 alert("Transcript report");
}
else
{
    alert("Not Transcript Report");
}

Unless otherwise, these contents are dynamically added, it is better to give this input an id, and access via it. 除非另有说明,否则将动态添加这些内容,最好为此输入提供一个ID,并通过它进行访问。

HTML entities are translated before they're shown in the browser, therefor &amp; HTML实体必须先翻译,然后才能在浏览器中显示&amp; becomes & and so forth. 变成&依此类推。 So when you're filtering the selector you're looking for something that's been translated into something else. 因此,当您筛选选择器时,您正在寻找已被翻译为其他内容的内容。

Here's the JSFiddle . 这是JSFiddle

Without getting into details of why you're selecting input elements this way, you just need to understand what I said and change the selector to: 无需深入了解为何以这种方式选择输入元素的细节,您只需要了解我的意思并将选择器更改为:

$("input[name='RETURN.URL'][value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']")

Check the value instead of using it as a selector. 检查值,而不是将其用作选择器。

JSFiddle JSFiddle

if ( $("input[name='RETURN.URL']").val().length > 0 )
{
    alert("Transcript report");
}
else
{
    alert("Not Transcript Report");
}

lose the dot in RETURN.URL (its not a preferred way of adding names with dots to input elements) 丢失RETURN.URL的点(这不是在输入元素中添加RETURN.URL名称的首选方式)

& use $('input[name=RETURNURL]').val().length &使用$('input[name=RETURNURL]').val().length

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

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