1

我正在尝试创建一个脚本,一旦选中 N/A (In Person Interview) 复选框,该脚本将在下拉字段中自动选择“In Person Interview”。我已经在这里阅读了近 2 周的示例,但没有找到任何可以为我指明正确方向的内容。任何帮助表示赞赏。谢谢。

这是我一直在尝试使用的小提琴

这是标记:

<li id="fielditem_51926" class="field toggleField">
    <table>
        <tbody>
            <tr>
                <td style="width: auto">
                    <input type="hidden" value="false" name="client[custom][custom70]">
                    <input id="field_51926" class=" field_51926 " type="checkbox" value="true" name="client[custom][custom70]">
                    <label id="field_51926_form_label">N/A·(In Person Interview)?·</label>
                    <div id="field_51926_form_note" class="global_note"></div>
                </td>
                <td></td>
            </tr>
        </tbody>
    </table>
</li>
<li id="fielditem_50508" class="field toggleField">?
    <table>
        <tbody>
            <tr>
                <td style="width: auto">
                    <label id="field_50508_form_label" class="above">
                        Need
                        <span class="alert">&nbsp;*</span>
                    </label>
                    <div id="field_50508_form_note" class="global_note">What Are You Looking For Today?</div>
                    <div id="container-50508" class="field_container">
                        <select id="field_50508" class="required_field " name="client[module][custom23][0][option_id]">
                            <option label="(select)" value="">(select)</option>
                            <option label="Utilities" value="45398">Utilities</option>
                            <option label="Motel Voucher" value="45947">Motel?·Voucher</option>
                            <option label="Health" value="49184">Health</option>
                            <option label="General Outreach Event" value="62620">General?·Outreach?·Event</option>
                            <option label="Email" value="46046">Email</option>
                            <option label="Disaster" value="45449">In Person Interview</option>
                        </select>
                    </div>
                </td>
                <td></td>
            </tr>
        </tbody>
    </table>
</li>
4

1 回答 1

1

如果这就是 HTML 的样子,那么这里有一个完整的脚本,它使用 jQuery 在选中该复选框时选择所需的选项。

请参阅 jsFiddle 中的底层代码。

// ==UserScript==
// @name     _Select matching option when CB checked, in poorly structured HTML
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==

var targChkBox = $("label:contains('(In Person Interview)')").prev ("input");
targChkBox.change (function () {
    if (this.checked) {
        var matchingOption  = $("option:contains('In Person Interview')");
        var targValue       = matchingOption.prop ("value");
        var targSelect      = matchingOption.parents ("select.required_field");
        targSelect.val (targValue); //-- Select the desired option.
    }
} );


假设字段和 ID 号是可变的,因此可以通过它们的文本找到关键元素(“In Person Interview”)。

请注意,HTML 看起来很可疑。例如,标签被滥用并且没有for属性。如果您在让此代码工作时遇到困难,请链接到实际的目标页面。

于 2012-06-27T05:35:45.163 回答