如何使用jquery找到此选择选项

时间:2016-03-24 21:49:02

标签: javascript jquery coldfusion coldfusion-11

我发现这个COLDFUSION代码用于选择列表以自动检查列表中的值,如果没有,则设置"默认"选中。只有它对我不起作用。 它进入cfif,脚本被放入页面,但是它永远不会发现JQUERY SELECTOR默认将它标记为SELECTED。

当页面首次运行时,

#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>

有人可以帮助我使用必要的选择器来查找此列表中的<option value="60" default="default">1 Hour</option>以及default="default"可能显示的任何其他列表。

工作代码在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>

1 个答案:

答案 0 :(得分:3)

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

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

那说使用prop()优于attr()以及使用类选择默认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);
});