我发现这个用于选择列表的 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 没有显示为 Default。
<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
block 正在执行 before `select 元素存在于页面中:
<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);
});
关于javascript - 如何使用 jquery 找到这个选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36210585/