单击提交按钮后如何保持下拉菜单的选定值 [英] How to remain selected value of dropdown after click on submit button

查看:52
本文介绍了单击提交按钮后如何保持下拉菜单的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击提交"后显示选定的数据.我搜索过多,但没有得到正确的答案.

I want to show selected data after click on submit.I have search too much but not get proper answer.

我使用了这段代码,但是下一步将是什么呢?

i used this code but what will be the next step i am not getting this.

if($actiondone!=''){
    echo "<script>
        $('#actiondoc').val();
        </script>";
}

请指导我解决方案

谢谢

推荐答案

您不能使用PHP与用户进行交互.

You cannot use PHP to interact with the user.

页面渲染完成后,PHP完成.如果您想与用户互动(例如,检测下拉菜单的更改等),则必须使用jQuery/javascript <仅 .

After the page has rendered, PHP is finished. If you want to interact with the user (i.e. detect changes to drop-downs, etc) you must use jQuery/javascript only.

以您的示例为例,您需要在页面中预先存在jQuery代码-也就是说,在最初呈现页面时,JavaScript会写入DOM中.您可以将jQuery/javascript代码添加为HTML的一部分,也可以在构建页面时在PHP中 echo 该代码-但是,当用户控制了页面时,它必须在页面上DOM.

For your example, you need to have the jQuery code pre-existing in the page - that is, the javascript gets written into the DOM as the page is being initially rendered. You can add the jQuery/javascript code as part of the HTML, or you can echo that code in PHP as the page is being built -- but it must be on the page when the user gets control of the DOM.

$(function(){
    $('#form_submit').click(function(){
        alert( $('#actiondoc').val() );
        return false; //this line cancels the SUBMIT action of the form
    });
    
    $('#actiondoc').change(function(){
      alert('You chose: ' + $(this).find('option:selected').text() );
    });
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<form id="your_form">
    <select id="actiondoc">
        <option value="">Choose:</option>
        <option value="chg">change of tyre</option>
        <option value="rnw">renew tyre</option>
    </select>
    <input id="form_submit" type="submit" value="submit" />
</form>

您说您想在用户单击提交"后显示选定的选项,但是该想法可能有问题:

You say that you want to show the selected choice after user clicks on submit, but there might be a problem with that idea:

当用户单击提交时,HTML表单将立即(a)获取表单控件的值,并且(b)将它们传递到< form action ="new_page.php"上指定的页面.method ="post"> -因此,在此处的这段代码中,浏览器将导航到 new_page.php 并将表单值传递到该页面.

When user clicks submit, an HTML form will immediately (a) get the values of the form controls and (b) deliver them to the page specified on the <form action="new_page.php" method="post"> -- so, in this code right here, the browser will navigate over to new_page.php and deliver the form values to that page.

重点是:页面将更改(除非您正在使用AJAX-是吗?)-由于用户不再在该页面上,因此您无法在该页面上显示更多内容

The point is: the page will change (unless you are using AJAX - are you?) - so you cannot show anything more on that page because the user isn't on that page any longer.

因此,您可以在新页面上显示 #actiondoc 控件的值, OR 您可以 拦截 提交功能,就像我在上面的演示中显示 alert()

So, you can show the value of the #actiondoc control on the new page, OR you can intercept the submit function, as I did in my demo above when I displayed the alert()

如果您确切地告诉我们有关您想要做什么的更多信息,我们可以提供更多帮助.

If you tell us more about exactly what you want to do, we can help more.

参考文献:

https://www.w3schools.com/xml/ajax_intro.asp

AJAX与表单提交

YouTube-简单的AJAX联系表单-HTML,PHP,AJAX

这篇关于单击提交按钮后如何保持下拉菜单的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆