我动态生成PHP中的值列表,我使用json_encode将其转换为json,如下所示:
<?php $posts = get_post_meta(get_the_id(), 'size_repeatable', false);
$specials = array();
foreach ( $posts as $post ) {
$size_available = $post['size_available'];
if(intval(get_the_title($size_available)) > 1) { $add_sq_ft = " sq ft room"; } else { $add_sq_ft = ""; }
$size_special_offer = $post['size_special_offer'];
$specials[] = array(get_the_title($size_available) . $add_sq_ft => $post['size_special_offer']);
}
$specials_simplified = array_reduce($specials, 'array_merge', array());
$specials_json = json_encode($specials_simplified);
?>
哪个产生这个JSON:
{"15 sq ft room":"10% off your first month","25 sq ft room":"15% off your first 1 month","35 sq ft room":"20% off your first 1 month","50 sq ft room":"25% off your first 1 month","75 sq ft room":"50% off your first 2 month","100 sq ft room":"50% off your first 2 month","125 sq ft room":"50% off your first 2 month","150 sq ft room":"50% off your first 2 months","200 sq ft room":"50% off your first 2 month"}
然后我有一个标准的HTML Select字段,其中包括15平方英尺的房间,25平方英尺的房间,35平方英尺的房间,50平方英尺的房间,75平方英尺的房间等作为可选择的选项(您只能选择一个选项)一次)。
然后我得到了一个.change jQuery函数,当更改时应该引用上面的JSON值,例如:
<script type="text/javascript">
jQuery(document).ready(function( $ ) {
$('#input_2_11').change(function(event) {
var current_selection = $('#input_2_11' + ' option:selected').html();
var size_json = <?php echo $specials_json; ?>;
$('#dynamic_offer').html(size_json);
});
});
</script>
基本上我需要将#input_2_11标签与JSON对象进行比较,以显示正确的值(特价)。
例如,如果用户选择了&#34; 15平方英尺的房间&#34;然后它会显示你的第一个月10%的折扣&#34;在#dynamic_offer div。
但这是我真正挣扎的地方 - 首先尝试比较变量&#39; current_selection&#39;与&#39; size_json&#39;然后显示与该大小相关的正确特价。
答案 0 :(得分:0)
你可以试试这个:
np.float32(0.2)
答案 1 :(得分:0)
您是否使用console.log()检查current_selection
包含的内容并验证size_json
的内容?
我认为current_selection
不会达到您的预期; .html()
将返回选项中的整个HTML,而您可能希望尝试$('#input_2_11').val()
之类的内容来获取输入的当前值。
获得current_selection
中的值后,您可以size_json[current_selection]
访问特价优惠,因为current_selection
中的值将是您需要的键值对的关键字来自size_json
。
<script type="text/javascript">
var size_json = <?php echo $specials_json; ?>;
jQuery(document).ready(function( $ ) {
$('#input_2_11').change(function() {
// If you need the option's value
//var current_selection = $('#input_2_11').val();
// If you just want the text of the option.
var current_selection = $('#input_2_11 option:selected').html();
$('#dynamic_offer').html(size_json[current_selection]);
});
});
</script>
答案 2 :(得分:0)
这是一个完整的例子。使用.html()
选项作为json
变量中的键。要使代码有效,只需将json
的作业替换为<?php echo $specials_json; ?>;
。
jQuery(document).ready(function() {
// For brevity
var size_json = {"15 sq ft room":"10% off your first month","25 sq ft room":"15% off your first 1 month","35 sq ft room":"20% off your first 1 month","50 sq ft room":"25% off your first 1 month","75 sq ft room":"50% off your first 2 month","100 sq ft room":"50% off your first 2 month","125 sq ft room":"50% off your first 2 month","150 sq ft room":"50% off your first 2 months","200 sq ft room":"50% off your first 2 month"};
// You don't need this part.
for (var key in size_json) {
if (size_json.hasOwnProperty(key)) {
$('#input_2_11').append($('<option/>').html(key));
}
}
function checkSelection() {
var current_selection = $('#input_2_11 option:selected').html();
$('#dynamic_offer').html(size_json[current_selection]); // Note the [current_selection]
}
checkSelection();
$('#input_2_11').change(checkSelection);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="input_2_11">
</select>
<div id="dynamic_offer">
</div>