2016-03-24 55 views
1

我發現這個COLDFUSION代碼的選擇列表自動檢查一個值的列表,如果沒有設置「默認」選定。只有它不適合我。 它進入cfif,腳本被放入頁面,但它從來沒有找到默認的JQUERY SELECTOR標記爲SELECTED。如何找到這個選擇選項使用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> 
+0

或許應該使用.prop(「選擇」,真正的),而不是.attr(「選擇」,「選擇」)......但jQuery的似乎無法使無論如何 – Tibrogargan

回答

3

您需要將代碼放入document.ready處理程序中,以便在DOM加載時執行它。目前正在執行的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); 
}); 
+0

廉價男人的版本:交換