簡體   English   中英

如何使用jquery找到此選擇選項

[英]How to find this Select Option using jquery

我發現這個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>

您需要將您的代碼放在document.ready處理程序中,以便在DOM加載時執行它。 目前,您的script塊正在頁面中存在`select元素之前執行:

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

也就是說,在attr()上使用prop()以及使用類選擇默認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);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM