简体   繁体   English

提交给SELF时“记住”表单删除列表值?

[英]'Remember' form drop list value when submitting to SELF?

I know how to 'remember' some form values whenever submitting the form to itself, in this case because of a picture upload function which requires the form to be submitted to itself. 我知道在将表单提交给自己时如何“记住”某些表单值,在这种情况下,由于图片上传功能需要将表单提交给自己。 I simply want it so that if the user has filled out all fields and then uploads an image, the form doesn't get resetted (cleared). 我只是想要它,以便如果用户填写了所有字段然后上传图像,则表单不会被重置(清除)。

I have solved this in regular fields and checkboxes like this: 我已经在常规字段和复选框中解决了此问题,如下所示:

<input type="text" name="headline" id="headline" value="<?php echo @$_POST['headline'];?>">

But how can I do this with drop lists? 但是我该如何使用下拉列表呢? or radio buttons? 或单选按钮? There is no value option in a 'SELECT' list, even though I have tried writing in value anyways in the SELECT statement. 即使我试图在SELECT语句中写入值,“选择”列表中也没有值选项。 Didn't work! 没工作!

So, how can I set the SELECT (drop down lists) value with PHP (OR JAVASCRIPT) ? 因此,如何使用PHP(或JAVASCRIPT)设置SELECT(下拉列表)值?

If you need more input let me know, thanks! 如果您需要更多输入,请告诉我,谢谢!

For selects, you need to compare each option to your posted value, and handle it individually. 对于选择,您需要将每个选项与您过帐的值进行比较,并分别进行处理。 Simply print out your options in a loop, and test each value against the value was was previously posted. 只需循环打印您的选项,然后针对先前发布的值测试每个值。 If it maches, add selected to the attributes of that particular option. 如果匹配,则将selected添加到该特定选项的属性。

$color = $_POST["colors"];
$colors = array("red","green","blue");

<select name="colors">
<?php foreach ($colors as $option) { ?>
  <option<?php print ($option == $color) ? " selected" : ""; ?>>
    <?php print $option; ?>
  </option>
<?php } ?>
</select>

Actually, found out that it is possible to set the selectedIndex with javascript... 实际上,发现可以使用javascript设置selectedIndex ...

So I could put the selectedIndex in a hidden input before submitting the form, and then get that selectedIndex and set it with a javascript function... tricky but suits me better in this case... 因此,我可以在提交表单之前将selectedIndex放入隐藏的输入中,然后获取该selectedIndex并使用javascript函数进行设置...棘手的但在这种情况下更适合我...

  document.getElementById("select").selectedIndex=nr;

Thanks though Jonathan! 谢谢乔纳森!

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

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