[英]Jquery-UI modal form not working properly
我有以下代碼:
<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>
問題在於,頁面加載時仍顯示表單,而不是在單擊按鈕並將其顯示為模式對話框之前將其隱藏。 我已經在頁面中加載了最新的JQuery和JQuery-UI。 知道可能是什么問題嗎?
<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>
請看這工作正常...
在將表單呈現到頁面之前會調用腳本,因此該腳本不起作用,因為$(“ #form_div”)找不到元素。 您可以將腳本放在$(document).ready()函數中或“ #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>
只需將其包裝在display:none div中(如果您不想顯示它)。 他們提供了一個選項,就是默認情況下頁面上只有一個對話框小部件。 您必須隱藏它才能使其不顯示。
讓按鈕具有id button
時,單擊它會打開對話框
<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>
還是在打開頁面時打開
<script type="text/javascript">
$(document).ready( function() {
$( "#form_div" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true
});
});
</script>
也許您的<script>
標記位於html中的<div>
之前 。 因此,當瀏覽器執行javascript時, <div>
尚未創建並且無法隱藏。 您可以將<script>
部分移到<div>
之后,也可以這樣寫:
<script type="text/javascript">
$(document).ready( function() {
$( "#form_div" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true
});
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.