简体   繁体   English

如何根据表单中输入的信息进行实时预览? PhP / ajax?

[英]How to make live preview based on information entered in a form? PhP/ajax?

I'm working on one project and it would be very handy to implement "live preview" feature in it. 我正在一个项目上,在其中实现“实时预览”功能将非常方便。 The problem is it is not some kind of typed text preview "eg one like on stackoverflow when you create a question". 问题是它不是某种类型的文本预览“例如,创建问题时类似stackoverflow的预览”。 What I am working on is slightly different. 我正在从事的工作略有不同。 For example, I have a form with inputs like (background color, padding value, margin value etc...) Each time user updates one of those inputs I wan't to show preview of updated object next to the form, Lets say this object is div styled basing on what user entered in a form. 例如,我有一个带有输入的表单,例如(背景色,填充值,边距值等)。每次用户更新其中一个输入时,我都不希望在表单旁边显示更新对象的预览,可以这样说对象是根据用户在表单中输入的内容进行div样式设置的。

How can I achieve this feature? 如何实现此功能?

jQuery based solution is preferable. 最好使用基于jQuery的解决方案。

Edit: say this is how form looks 编辑:说这是表格的样子

<form id="div_builder">
   <input type="text" name="background_color"/>
   <input type="text" name="padding"/>
   <input type="text" name="margin"/>
</form>

the change event is your friend. 变更事件是您的朋友。 On each change, call a function that assigns the chosen attributes to the div you want to display. 每次更改时,调用一个函数,将选定的属性分配给要显示的div。

not testet but should work (with jquery)... 不是testet,但应该可以(与jQuery)...

<form id="div_builder">
   <input type="text" name="background-color"/>
   <input type="text" name="padding"/>
   <input type="text" name="margin"/>
</form>

<div id="object"><img src="some object" /></div>

<script type="text/javascript">
$('#div_builder input').change(function(){
   var css = $(this).attr("name");
   $("#object").css(css,$(this).val());
})
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM