我使用BootstrapValidator作为我的验证组件。我需要给它从PHP聚集到它的构造类似这样的选择:如何将选项从PHP传递到Javascript?
PHP代码生成领域:
$fields = "";
foreach ($form["questions"] as $key => $value) {
if (!empty($value["validators"])) {
$fields .= "\"" . $key . "\":" . json_encode($value["validators"]) . ",";
}
}
$new_fields = "{" . chop($fields, ",") . "}";
的Javascript部分是:
$('#my_form').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: <?php echo $new_fields; ?>
});
它打印出这样的事情:
$('#fc_register_form').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
"field_1": {"notEmpty": {"message": "The ****** cannot be empty"}},
"field_1_confirm": {
"notEmpty": {"message": "The ******* must be same with first input for ******."},
"identical": {
"field": "field_1",
"message": "The given value is not same with first input for ****"}
},
"field_2": {"notEmpty": {"message": "The ******* empty"}},
"field_3": {"notEmpty": {"message": "The ******* empty"}},
"field_4": {"notEmpty": {"message": "The ****** is required and cannot be left empty"}}
}
});
那么,可能双引号导致它不起作用?或者那有什么问题?这应该不是那么多,我猜...
什么是'$ fields'?这是'json_encode()'的结果吗?附:你的示例在''message“中缺少''':”...“# – 2014-11-03 21:48:02
'$ fields'缺少它,或者你的复制/粘贴在'field_1_confirm'下的'message'丢失了'''选项。 – Kyle 2014-11-03 21:48:44
伙计们,我已经添加了PHP部分并修复了缺失“。感谢您的警告。 – 2014-11-03 21:51:29