如何使用 jquery 找到此 Select 选项

How to find this Select Option using jquery

我发现这个用于 Select 列表的 COLDFUSION 代码可以自动检查列表中的值,如果没有,则将 "the default" 设置为选中。只是它对我不起作用。 它进入 cfif,脚本被放入页面,但是它从来没有找到默认的 JQUERY 选择器将其标记为已选择。

当页面首次运行时,

#igShutdownTimer option[default="default"] 没有将 value="60" 标记为已选中。

<div class="col-md-12">
    <label for="igShutdownTimer">Ignition ShutDown Timer:</label>
    <cfset pos = listfind(inputList,"1_uint ")>
    <cfif not pos>
        <script>
            $('##igShutdownTimer option[default="default"]').attr('selected','selected');
        </script>
    </cfif>
    <select form="configDetail" id="igShutdownTimer" name="1_uint" class="form-control" changeName="Ignition ShutDown Timer">
        <option value="0" <cfif pos and getParams.value[pos] is "0">selected</cfif>>0 Minutes</option>
        <option value="10" <cfif pos and getParams.value[pos] is "10">selected</cfif>>10 Minutes</option>
        <option value="20" <cfif pos and getParams.value[pos] is "20">selected</cfif>>20 minutes</option>
        <option value="30" <cfif pos and getParams.value[pos] is "30">selected</cfif>>30 Minutes</option>
        <option value="40" <cfif pos and getParams.value[pos] is "40">selected</cfif>>40 Minutes</option>
        <option value="50" <cfif pos and getParams.value[pos] is "50">selected</cfif>>50 Minutes</option>
        <option value="60" <cfif pos and getParams.value[pos] is "60">selected</cfif>  default="default">1 Hour</option>
        <option value="120" <cfif pos and getParams.value[pos] is "120">selected</cfif>>2 hours</option>
        <option value="240" <cfif pos and getParams.value[pos] is "240">selected</cfif>>4 Hours</option>
        <option value="480" <cfif pos and getParams.value[pos] is "480">selected</cfif>>8 Hours</option>
        <option value="720" <cfif pos and getParams.value[pos] is "720">selected</cfif>>12 hours</option>
        <option value="1440" <cfif pos and getParams.value[pos] is "1440">selected</cfif>>24 Hours</option>
        <option value="4294967295" <cfif pos and getParams.value[pos] is "4294967295">selected</cfif>>Unlimited</option>
    </select>
</div>

这是打印到源的内容,如您所见,SELECTED 不会显示为默认值。

<script>
    $('#igShutdownTimer option[default="default"]').attr('selected','selected');
</script>
<select form="configDetail" id="igShutdownTimer" name="9X26889E92489D" class="form-control" changeName="Ignition ShutDown Timer">
    <option value="0" >0 Minutes</option>
    <option value="10" >10 Minutes</option>
    <option value="20" >20 minutes</option>
    <option value="30" >30 Minutes</option>
    <option value="40" >40 Minutes</option>
    <option value="50" >50 Minutes</option>
    <option value="60" default="default">1 Hour</option>
    <option value="120" >2 hours</option>
    <option value="240" >4 Hours</option>
    <option value="480" >8 Hours</option>
    <option value="720" >12 hours</option>
    <option value="1440" >24 Hours</option>
    <option value="4294967295" >Unlimited</option>
</select>

谁能帮我找到必要的选择器,以便在此列表和 default="default" 可能出现的任何其他列表中找到 <option value="60" default="default">1 Hour</option>


Rory 回答后的工作代码:

<div class="col-md-12">
    <label for="igShutdownTimer">Ignition ShutDown Timer:</label>
    <cfset pos = listfind(inputList,"1_uint ")>
    <select form="configDetail" id="igShutdownTimer" name="#daCFC.enc('1_uint')#" class="form-control" changeName="Ignition ShutDown Timer">
        <option value="0" <cfif pos and getParams.value[pos] is "0">selected</cfif>>0 Minutes</option>
        <option value="10" <cfif pos and getParams.value[pos] is "10">selected</cfif>>10 Minutes</option>
        <option value="20" <cfif pos and getParams.value[pos] is "20">selected</cfif>>20 minutes</option>
        <option value="30" <cfif pos and getParams.value[pos] is "30">selected</cfif>>30 Minutes</option>
        <option value="40" <cfif pos and getParams.value[pos] is "40">selected</cfif>>40 Minutes</option>
        <option value="50" <cfif pos and getParams.value[pos] is "50">selected</cfif>>50 Minutes</option>
        <option value="60" class="default" <cfif pos and getParams.value[pos] is "60">selected</cfif>>1 Hour</option>
        <option value="120" <cfif pos and getParams.value[pos] is "120">selected</cfif>>2 hours</option>
        <option value="240" <cfif pos and getParams.value[pos] is "240">selected</cfif>>4 Hours</option>
        <option value="480" <cfif pos and getParams.value[pos] is "480">selected</cfif>>8 Hours</option>
        <option value="720" <cfif pos and getParams.value[pos] is "720">selected</cfif>>12 hours</option>
        <option value="1440" <cfif pos and getParams.value[pos] is "1440">selected</cfif>>24 Hours</option>
        <option value="4294967295" <cfif pos and getParams.value[pos] is "4294967295">selected</cfif>>Unlimited</option>
    </select>
</div>
<cfif not pos>
    <script>
        $('##igShutdownTimer option.default').prop('selected', true);
    </script>
</cfif>

您需要将代码放在 document.ready 处理程序中,以便在 DOM 加载时执行。目前您的 script 块正在执行 页面中存在 `select 元素之前:

<script>
    $(function() {
        $('#igShutdownTimer option[default="default"]').attr('selected','selected');
    });
</script>

也就是说,使用 prop() 而不是 attr() 以及使用 class select 默认 option 会更好,因为创建您自己的非标准属性将意味着您的 HTML 无效。像这样:

<select form="configDetail" id="igShutdownTimer" name="9X26889E92489D" class="form-control">
    <option value="0">0 Minutes</option>
    <!-- other options... -->
    <option value="60" class="default">1 Hour</option>
</select>
$(function() {
    $('#igShutdownTimer option.default').prop('selected', true);
});