gpt4 book ai didi

php - jQuery 中未显示页面加载默认值?

转载 作者:行者123 更新时间:2023-11-28 00:09:39 24 4
gpt4 key购买 nike

我有五个单选按钮,根据单选按钮的五个不同值,生成了五个跨度标记,其中包含此单选按钮的描述,如下所示:

<table class="jshop">
<tbody>
<tr>
<td class="attributes_title">
<span class="attributes_name">Model:</span><span class="attributes_description"></span>
</td>
<td>
<span id="block_attr_sel_3">
<span class="input_type_radio">
<input type="radio" name="jshop_attr_id[3]" id="jshop_attr_id35" value="5" checked="true" onclick="setAttrValue('3', this.value);">
<label for="jshop_attr_id35"><span class="radio_attr_label">16 GB</span></label>
</span>
<span class="input_type_radio">
<input type="radio" name="jshop_attr_id[3]" id="jshop_attr_id36" value="6" onclick="setAttrValue('3', this.value);">
<label for="jshop_attr_id36"><span class="radio_attr_label">32 GB</span></label></span>
</span>
</td>
</tr>
<tr>
<td class="attributes_title">
<span class="attributes_name">What Type?:</span><span class="attributes_description"></span>
</td>
<td>
<span id="block_attr_sel_5">
<span class="input_type_radio">
<input type="radio" name="jshop_attr_id[5]" id="jshop_attr_id59" value="9" checked="true" onclick="setAttrValue('5', this.value);">
<label for="jshop_attr_id59"><span class="radio_attr_label">Broken</span></label>
</span>
<span class="input_type_radio">
<input type="radio" name="jshop_attr_id[5]" id="jshop_attr_id510" value="10" onclick="setAttrValue('5', this.value);">
<label for="jshop_attr_id510"><span class="radio_attr_label">Good</span></label>
</span>
<span class="input_type_radio">
<input type="radio" name="jshop_attr_id[5]" id="jshop_attr_id511" value="11" onclick="setAttrValue('5', this.value);">
<label for="jshop_attr_id511"><span class="radio_attr_label">Flawless</span></label></span>
</span>
</td>
</tr>
</tbody>
</table>

<span id="attr_5" class="attr_desc"></span>
<span id="attr_6" class="attr_desc"></span>
<span id="attr_9" class="attr_desc">test1</span>
<span id="attr_10" class="attr_desc">test2</span>
<span id="attr_11" class="attr_desc">test3</span>

现在我希望每当用户从上面选择一个单选按钮时,相关描述就会显示。所以我这样尝试:

<script>
jQuery(document).ready(function() {

jQuery(".input_type_radio input[type=radio]").change(function() {
jQuery(".attr_desc").hide();
var test = jQuery(this).val();

jQuery("#attr_" + test).show();
});
jQuery(".attr_desc").hide();
});
</script>

它运行良好,但我的问题是无论何时加载页面,都不会显示任何描述,即使默认情况下已经选择了两个值,我不知道为什么它不起作用。请给我建议。

最佳答案

那是因为你只在更改事件中显示/隐藏相应的跨度......你需要检查 document.ready 中选中的值并同样显示/隐藏

试试这个

 jQuery(document).ready(function() {

jQuery(".input_type_radio input[type=radio]").change(function() {
..... //your code
});
jQuery(".input_type_radio input[type=radio]:checked").each(function(){
jQuery("#attr_" + $(this).val()).show();
});

});

working fiddle here

关于php - jQuery 中未显示页面加载默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16102372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com