[英]Pre-fill a form (no control over form code)
I'm using a form plugin / addon where I don't have control over how the form is generated. 我使用的是表单插件/插件,无法控制表单的生成方式。
The form generates HTML which looks like this: 表单生成如下所示的HTML:
<form>
<label>First Name</label><input name="Question25" type="text" value="" />
<input class="formBlockSubmitButton" name="add" type="submit" value="Submit" />
</form>
I would like to prefill some of the form values but I can't write, say 我想预填一些表单值,但是我不能写
<input name="Question25" type="text" value="<?php echo $my_value; ?>" />
Because the form addon generates that HTML. 因为表单插件会生成该HTML。 I can write PHP or Javascript before this block, is there a way to search for an input with a name of "Question25" and then prefill it when the block comes up? 我可以在该块之前编写PHP或Javascript,有没有一种方法可以搜索名称为“ Question25”的输入,然后在出现该块时预填充它?
You could use jQuery and the .val()
method: 您可以使用jQuery和.val()
方法:
<script type="text/javascript">
$(function() {
$('input[name="Question25"]').val('some value');
});
</script>
and if the value must come from the server: 并且该值必须来自服务器:
<script type="text/javascript">
$(function() {
var obj = <?php echo json_encode(array('value' => $my_value)); ?>;
$('input[name="Question25"]').val(obj.value);
});
</script>
You can emit all the values you'd like to search as an array of fieldname/values, and then emit a function that tries to map those values to those fields. 您可以将所有要搜索的值作为字段名/值的数组发出,然后发出尝试将这些值映射到那些字段的函数。
<script type="text/javascript">
var values = [{fieldName:'Question25', value:'MyValue'}]; // iterate through your values in PHP and generate the {fieldName:'value',value:'value'} object
$(function() {
$.each(values, function(index, item) {
$('input[name=' + item.fieldName + ']').val(item.value);
});
});
</script>
var value='<?php echo $my_value; ?>'
$("input:text[name='Question25']").val(value);
use something like this 用这样的东西
Use Javascript. 使用Javascript。 With jQuery, you could do something along the lines of: 使用jQuery,您可以执行以下操作:
$("input[name=Question25]").val(my_value);
You can then insert PHP vairables into the Javascript with echo, load them with AJAX, or whatever you need. 然后,您可以通过echo将PHP vairable插入到Javascript中,并使用AJAX加载它们,或根据需要进行加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.