简体   繁体   中英

Jquery-UI modal form not working properly

I have the following code:

    <script type="text/javascript">
        $( "#form_div" ).dialog({
            autoOpen: false,
            height: 300,
            width: 350,
            modal: true
        });
    </script>
<div id="form_div" title="Trimite-ne un mesaj!">
    <form name="htmlform" method="post" action="html_form_send.php">
        <table width="450px">
            </tr>
            <tr>
                <td valign="top">
                    <label for="first_name">Nume*</label>
                </td>
                <td valign="top">
                    <input  type="text" id="first_name" maxlength="50" size="30">
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="email">Email *</label>
                </td>
                <td valign="top">
                    <input  type="text" id="email" maxlength="80" size="30">
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="message">Mesaj: *</label>
                </td>
                <td valign="top">
                    <textarea  id="message" maxlength="1000" cols="25" rows="6"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

The problem is that the form is still displayed when the page loads instead of being hidden until a button is clicked and displayed as modal dialog. I have loaded the latest JQuery and JQuery-UI in my page. Any idea what the problem might be?

<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Dialog - Default functionality</title>
<link rel="stylesheet"
 href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
function OpenModal()
{
$( "#form_div").dialog();
$("#btnDisp").hide();
}
</script>
</head>
<body>

<div id="form_div" title="Trimite-ne un mesaj!" style="display:none;">
    <form name="htmlform" method="post" action="html_form_send.php">
        <table width="450px">
            </tr>
            <tr>
                <td valign="top">
                    <label for="first_name">Nume*</label>
                </td>
                <td valign="top">
                    <input  type="text" id="first_name" maxlength="50" size="30">
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="email">Email *</label>
                </td>
                <td valign="top">
                    <input  type="text" id="email" maxlength="80" size="30">
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label for="message">Mesaj: *</label>
                </td>
                <td valign="top">
                    <textarea  id="message" maxlength="1000" 
              cols="25" rows="6"></textarea>
                </td>
            </tr>
        </table>        
    </form> 
    </div>
    <input id="btnDisp" type="button" onclick="OpenModal();" 
value="display the form"/>

</body>
</html>

Please see this working perfectly...

The script is called before your form is rendered to page, so it doesn't work because $( "#form_div") can not find the element. You can put your script in $(document).ready() function or after "#form_div".

<div id="form_div" title="Trimite-ne un mesaj!">  
...
</div>
<script type="text/javascript">
    $( "#form_div" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true
    });
</script>

Just wrap this in a display:none div if you don't want it displayed. They offer the option to just have a dialog widget that's on the page by default. You have to hide it your own for it to not show up.

let the button has a id button when it will click it will open the dialog

<div id="form_div" title="Trimite-ne un mesaj!" style="display:none;">
 ...
</div>

<input type="button" id="button"/>

 <script type="text/javascript">
   $(document).on('click', '#button', function(){ $( "#form_div" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true
    });});
</script>

or if it will open while page is opened

 <script type="text/javascript">
   $(document).ready( function() {
    $( "#form_div" ).dialog({
    autoOpen: false,
    height: 300,
    width: 350,
    modal: true
   });
   });
  </script>

Maybe your <script> tag comes before the <div> in your html. So when the browser executes the javascript, the <div> is not yet created and cannot be hidden. Either you move the <script> part after the <div> or you write it like that:

<script type="text/javascript">
  $(document).ready( function() {
    $( "#form_div" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true
    });
  });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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