使用jQuery,我需要确定字段集中的最后一个可见输入

时间:2015-10-15 18:04:20

标签: jquery jquery-selectors

我有一个显示我的问题的codepen:http://codepen.io/esdictor/pen/KdqeEV

结构:我有一个包含字段集的表单。每个字段集都包含FormRows(div class =“FormRow”)。每个FormRow都包含Fields(div class =“FormField”)。每个字段通常包含一个表单元素(输入,选择,文本区域等,但对于此示例,我将坚持输入)。所以:

<form>
    <fieldset>
        <legend>Group 1</legend>
        <div class="FormRow">
            <div class="FormField">
                <label for="Test1">Test1</label>
                <input type="text" id="Test1" />
            </div>
            <div class="FormField">
                <label for="Test2">Test2</label>
                <input type="text" id="Test2" />
            </div>
        </div>
    </fieldset>
</form>>

要求:我的页面需要对每个字段集中的最后一个输入做出反应,但最终字段集除外。我想出了一个最初工作正常的选择器:

$('form fieldset:not(:last-of-type) .FormRow:last-of-type .FormField:last-of-type input')

问题:最近我获得了一些可能基于表单中的其他数据隐藏的新元素,如果这些元素位于字段集的末尾并且隐藏了WERE,我的代码不再有效。

我的示例:上面链接的codepen在Group 2字段集中有一个隐藏的TextA输入(实际上,隐藏了整个FormRow)。因为这是隐藏的,Test9输入应该是红色的。但是,我无法想出一个选择器来考虑这一点。

注意此表单是数据驱动的,用于许多不同类型的数据,因此我无法对任何内容进行硬编码。我需要“知道”控件是字段集中的最后一个可见控件,以便我可以相应地编写我的事件。

一如既往,提前谢谢!

2 个答案:

答案 0 :(得分:1)

问题在于,当您选择上一个public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { // Check if this was launched by double-clicking a doc. If so, use that as the // startup file name. string fname = "No filename given"; if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0) { try { fname = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]; // It comes in as a URI; this helps to convert it to a path. Uri uri = new Uri(fname); fname = uri.LocalPath; this.Properties["ArbitraryArgName"] = fname; } catch { // For some reason, this couldn't be read as a URI. // Do what you must... } } string procName = Process.GetCurrentProcess().ProcessName; // get the list of all processes by the "procName" Process[] processes = Process.GetProcessesByName(procName); if (processes.Length > 1) { MessageBox.Show(procName + " already running"); //App a = new App(); //a.Shutdown(); } else { } MainWindow mainWindow = new MainWindow(); mainWindow.Show(); base.OnStartup(e); } } (或上一个FormField的事件)时,它无法显示任何可见的输入。

你可以这样做:

FormRow

或者避免使用非常复杂的选择器:

$('form fieldset:not(:last-of-type) .FormRow:last-of-type:has(input:visible) .FormField:last-of-type:has(input:visible) input:visible')

根据评论,您还可以使用:

$('form fieldset:not(:last-of-type)').each(function() {
    var $fieldset = $(this),
        $input = $fieldset.find("input:visible:last");
    if ($input.length > 0)
        // Do your work here
        $input.css("background-color", "red");
});

答案 1 :(得分:0)

您可以尝试使用&#34;:可见&#34;选择器。

$('form fieldset:not(:last-of-type) .FormRow:last-of-type .FormField:last-of-type input:visible')

或者如果它隐藏了FormField:

$('form fieldset:not(:last-of-type) .FormRow:last-of-type .FormField:last-of-type:visible input')