尝试使用jQuery和PHP显示模式对话框

时间:2011-03-08 15:27:02

标签: jquery modal-dialog

我有一个电子邮件按钮,点击后,会弹出一个模态jQuery UI对话框,显示一个用于通过电子邮件发送给自己的按钮。我的代码在这里:

<input type="button" value="Email to me" onClick="$("#emailModal").dialog({title:'Email It Along',height:300,width:350,modal:true,resizable:false});"/>

这是#emailModal

<div id="emailModal">
<p><input type="button" value="Email to me" onClick="$('#emailPost').submit()" id="emailJQButton"/></p>
</div>

但是,当我点击按钮时,没有任何反应,也没有弹出对话框。我查看了所有代码,但找不到问题。有什么帮助吗?

修改: 发现了问题,但我还有一个问题:

if($_SESSION["loggedIn"] == 1)
                        {
                            echo "<form action='php/emailPost.php' method='POST' class='inline' id='emailPost'>";
                            echo "<input type='hidden' value='" . $_SESSION["email"] . "' name='emailAddress'>";
                            echo "<input type='button' value='Email To Me' onClick='$(\"#emailPost\").submit();$(\"#emailModal\").dialog('close');'/>";
                            echo "<input type='hidden' name='passedCoupID' value='" . $coupID . "'/>";
                            echo "</form>";
                            echo "<h3>Or</h3>";
                            echo "<form action='php/emailPost.php' method='POST' class='inline' id='emailPost2'>";
                            echo "<input type='text' value='Enter an Email' name='emailAddress' style='display: inline-block;'>";
                            echo "<input type='button' value='Email' onClick='$(\"#emailPost2\").submit();$(\"#emailModal\").dialog('close');'/>";
                            echo "<input type='hidden' name='passedCoupID' value='" . $coupID . "'/>";
                            echo "</form>";
                        }
                        else
                        {
                            echo "<form action='php/emailPost.php' method='POST' class='inline' id='emailPost2'>";
                            echo "<input type='text' value='Enter an Email' name='emailAddress' style='display: inline-block;'>";
                            echo "<input type='button' value='Email' onClick='$(\"#emailPost2\").submit();$(\"#emailModal\").dialog('close');'/>";
                            echo "<input type='hidden' name='passedCoupID' value='" . $coupID . "'/>";
                            echo "</form>";
                        }

我现在用PHP echo显示它,我知道应该需要一些特殊的转义规则。内联功能都不起作用,不是emailPost提交,也不是关闭对话框。

4 个答案:

答案 0 :(得分:6)

如果引用",则无法在属性中使用"。使用'#emailModal'代替"#emailModal"

然而,正确的解决方案是:

<input type="button" value="Email to me" id="emailButton" />
<script type="text/javascript">
    $('#emailButton').click(function(e) {
        e.preventDefault();
        $("#emailModal").dialog({title:'Email It Along', height:300, width:350, modal:true, resizable:false});
    });
</script>

答案 1 :(得分:1)

您的代码中有拼写错误(撇号)

onClick="$("#emailModal").

应该是

onClick="$('#emailModal').

答案 2 :(得分:1)

问题出在onClick按钮上,双引号内有双引号。

onClick="$("#emailModal").dialog({...

我建议您不要使用onClick,而是使用jQuery来绑定事件。

<input type="button" value="Email to me" id="sendEmail" />

<div id="emailModal">
  <p><input type="button" value="Email to me" id="emailJQButton"/></p>
</div>

<script>
  $(function(){
    $("#emailModal").dialog({
      title: 'Email It Along',
      height: 300,
      width: 350,
      modal: true,
      resizable: false,
      autoOpen: false
    });

    $('#sendEmail').click(function(){
      $("#emailModal").dialog('open');
    });

    $('#emailJQButton').click(function(){
      $('#emailPost').submit();
    });
  });
</script>

答案 3 :(得分:0)

OTOH我认为你应该使用

<input type="button" value="Email to me" onClick="$('#emailModal').dialog({title:'Email It Along',height:300,width:350,modal:true,resizable:false});"/>

修改

或者正如ThiefMaster所说,尝试使用

中的内容
$(function(){
    $('#myinput').click(function(){
        $('#emailModal').dialog({title:'Email It Along',height:300,width:350,modal:true,resizable:false})
    });
});