简体   繁体   English

单击按钮时仅显示输入框

[英]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.

相关问题 当我单击按钮时,它仅在第一个显示输出div&#39;mouse_move&#39;时在jQuery ajax上显示图像,而在另一次单击上不显示相同图像 - When I click button it display image with jQuery ajax on output div 'mouse_move' only for first one but not display same on another click 显示日期 (<!--?php echo date('Y-m-d');?--> ;) 单击按钮时输入 - JQuery JS PHP - Display date (<?php echo date('Y-m-d');?>;) to input when button is click - JQuery JS PHP 当单击按钮时,while循环仅显示数据库中的最后一行[以显示模式对话框] - while loop shows only the last row in database when click at button[to display modal dialog] ajax调用仅显示每个循环中第一项的输入值。 需要它显示取决于按钮单击的上下文 - ajax call only showing input values for the first item in for each loop. Need it to display depending on context of button click 在自动完成输入上书写时,显示/隐藏按钮 - Display/Hide button when writing on autocomplete input 如何仅显示3条评论,然后每个按钮单击再显示10条评论 - How to display only 3 comments and then 10 more comments on each button click jQuery在单击关闭按钮时仅会隐藏该特定的div吗? - jQuery when click on the close button it will only hide that particular div only? 单击“特定”按钮时如何显示特定值 - How to Display Specific value when I click Specific button 当我们在浏览器中单击后退按钮时,显示上一页结果 - Display previous page results when we click back button in browsers 如何在加载了jQuery Media Boxs库时显示框/图像 - How to display boxes / images as an when it loaded with jquery media boxes library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM