[英]only display input boxes when button is click
I have a form with many potential input boxes, however I only want them to be displayed if the button next to the previous input box is clicked. 我的表单中有很多潜在的输入框,但是,我只希望在单击上一个输入框旁边的按钮时显示它们。 Here's a little Idea of what I mean: 这是我的意思的一点想法:
<form>
<input name='box1'><input type='submit' name='button1' value='show box 2'>
<input name='box2'><input type='submit' name='button2' value='show box 3'>
<input name='box3'><input type='submit' name='button3' value='show box 4'> .......
and so on 等等
Can this be done with PHP or should I use JavaScript or something else? 可以使用PHP完成此操作,还是应该使用JavaScript或其他方法? Any advice would be much appreciated. 任何建议将不胜感激。 Thanks 谢谢
I would use jQuery or pure JavaScript to do this. 我会使用jQuery或纯JavaScript来做到这一点。 JavaScript is a client side language which is ideal for picking up clicks and events. JavaScript是一种客户端语言,非常适合获取点击和事件。 PHP is server side and is used for page processing and data handling. PHP是服务器端,用于页面处理和数据处理。 I would wrap the inputs in divs and hide and show them on clicks. 我会将输入包装在div中,并在单击时隐藏并显示它们。
Yes this can be done by PHP, but if you want it look nice use javascript. 是的,这可以通过PHP完成,但是如果您希望它看起来不错,请使用javascript。
Anyways, an ugly pseudo solution in php: 无论如何,PHP中的一个丑陋的伪解决方案:
$Step = 1;
if(isset($_POST['step']))
$Step = $_POST['step'];
switch($Step) {
case 2:
echo '<input name="box2">';
break;
case 3:
echo '<input name="box3">';
break;
// ...
default:
echo '<input name="box1">';
}
echo '<input type="hidden" name="step" value="'.($Step + 1).'">';
echo '<input type="submit" name="submit" value="Next">';
But still, in javascript it's looking better and it's easier to implement. 但是,在javascript中,它看起来更好并且易于实现。
If you want to avoid page reloads, you need to use browser side JavaScript for showing the new input boxes. 如果要避免页面重新加载,则需要使用浏览器端JavaScript来显示新的输入框。
If you then need the text of the input boxes to be processed by your server side PHP scripts before the new input box shows up, you will need to make ajax requests on button clicks to send the data of the input box to the server and get a response. 如果然后在新输入框出现之前,需要服务器端PHP脚本处理输入框的文本,则需要在单击按钮时发出Ajax请求,以将输入框的数据发送到服务器并获取一个答复。
If you can process the text of the input boxes browser sided, it is getting simpler. 如果您可以在浏览器端处理输入框的文本,它将变得越来越简单。
In both cases you will want to replace the type attribute submit
of the buttons with button
to get buttons without default behaviour. 在这两种情况下,您都希望将按钮的type属性submit
替换为button
以获取没有默认行为的按钮。 You can then add click handlers to the buttons. 然后,您可以将单击处理程序添加到按钮。 With jQuery this would look like the following snippet, if you don't want to process the text (put this after your form): 使用jQuery,如果您不希望处理文本(将其放在表单后),则类似于以下代码片段:
<script>
$('input[type^="button"]').click(function() {
var button_number = $(this).attr("name").split("button")[1];
var box_number = parseInt(button_number) + 1;
$('input[name="box' + box_number + '"]').show();
});
</script>
For the initialization of the visibility, you can use this CSS: 为了初始化可见性,可以使用以下CSS:
input[name^="box"] {
display:none;
}
input[name="box1"] {
display:inline;
}
here is the jsfiddle: http://jsfiddle.net/SjXH9/27/ 这是jsfiddle: http : //jsfiddle.net/SjXH9/27/
you can make your last button a submit button, to submit the whole form. 您可以将最后一个按钮设为提交按钮,以提交整个表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.