以下は、モーダルウィンドウを閉じるHtmlボタンとjavascript関数です。
<button type="button" id="close" onclick="return parent.closewindow();">Close</button>
function closewindow() {
$.modal.close();
return false;
}
これで、上記のコードはモーダルウィンドウを閉じます。これは問題ありません。
しかし、私はこの関数と以下のhtmlボタンを使用してコードを書く方法を知りたいです:
<button type='button' class='add'>Add</button>
$(document).on('click', '.add', function(){
$.modal.close(); ;
return true;
});
上記のコードはモーダルウィンドウを閉じませんが、これはなぜですか?ちなみに、これらのコードは両方とも同じページにあります。
ericmartinが開発したsimplemodalを使用しています
アップデート:
以下は完全なコード(QandATable2.php)ですが、[追加]ボタンをクリックしてもモーダルウィンドウが閉じません。
$(document).ready(function(){
$('.add').on('click', function(){
//close modal window when user clicks on "Add" button (not working)
$.modal.close();
});
});
function plusbutton() {
// Display an external page using an iframe
var src = "previousquestions.php";
$.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
//Opens modal window and displays an iframe which contains content from another php script
return false;
}
function closewindow() {
$.modal.close();
return false;
//closes modal window when user clicks on "Close" button and this works fines
}
...HTML
<table id="plus" align="center">
<tr>
<th>
<a onclick="return plusbutton();">
<img src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" name="plusbuttonrow"/>
</a>
<span id="plussignmsg">(Click Plus Sign to look <br/> up Previous Questions)</span>
</th>
</tr>
</table>
以下は、モーダルウィンドウに入るすべての情報を表示する完全なコードです(これはpreviousquestions.phpスクリプトにあります)。これには、「追加」ボタンと「閉じる」ボタンの両方が含まれます。
<div id="previouslink">
<button type="button" id="close" onclick="return parent.closewindow();">Close</button>
</div>
<?php
$output = "";
while ($questionrow = mysql_fetch_assoc($questionresult)) {
$output .= "
<table>
<tr>
<td id='addtd'><button type='button' class='add'>Add</button></td>
</tr>";
}
$output .= " </table>";
echo $output;
?>