提交不执行提交功能

时间:2014-02-20 16:45:41

标签: javascript html function submit onsubmit

我期待提交按钮执行功能检查器()然而它根本不工作,没有警报,我做错了什么?

应该是我按提交并且检查器继续,如果电子邮件字段正确,则会得到真或假。如果是,则出现弹出div。

或者我想做的就是提交使用的表格:

<form action="<?php echo $website.$relative_string;?>" name="subscribe" onsubmit="javascript:return checkEmail(this);" method="post">

提交之后,我将调用函数popupClick();

谢谢Chris

<?php include("globals.php"); ?>   
<script>
var sub = document.getElementById('sub');
var unsub = document.getElementById('unsub');
var btn = document.getElementById('submitButton2');

jQuery(function($) {

$("a.topopup").click(function() {
        loading(); // loading
        setTimeout(function(){ // then show popup, deley in .5 second
            loadPopup(); // function show popup 
        }, 500); // .5 second
return false;
});

/* event for close the popup */
$("div.close").hover(
                function() {
                    $('span.ecs_tooltip').show();
                },
                function () {
                    $('span.ecs_tooltip').hide();
                }
            );

$("div.close").click(function() {
    disablePopup();  // function close pop up
});

$(this).keyup(function(event) {
    if (event.which == 27) { // 27 is 'Ecs' in the keyboard
        disablePopup();  // function close pop up
    }   
});

$("div#backgroundPopup").click(function() {
    disablePopup();  // function close pop up
});

function popupClick() {
    loading(); // loading
    setTimeout(function(){ // then show popup, deley in .5 second
        loadPopup(); // function show popup 
    }, 500); // .5 second
}

/************** start: functions. **************/
function loading() {
    $("div.loader").show();  
}
function closeloading() {
    $("div.loader").fadeOut('normal');  
}

var popupStatus = 0; // set value

function loadPopup() { 
    if(popupStatus == 0) { // if value is 0, show popup
        closeloading(); // fadeout loading
        $("#toPopup").fadeIn(0500); // fadein popup div
        $("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
        $("#backgroundPopup").fadeIn(0001); 
        popupStatus = 1; // and set value to 1
    }   
}

function disablePopup() {
    if(popupStatus == 1) { // if value is 1, close popup
        $("#toPopup").fadeOut("normal");  
        $("#backgroundPopup").fadeOut("normal");  
        popupStatus = 0;  // and set value to 0
    }
}
/************** end: functions. **************/

sub.onchange = function() //When sub changes
{
if(sub.checked)  //If it's checked
{
    btn.innerHTML = "<span>Join</span>";
}
else // If not..
{
    btn.innerHTML = "<span>Leave</span>";
}
}

unsub.onchange = function() //When unsub changes
{
if(unsub.checked)  //If it's checked
{
    btn.innerHTML = "<span>Leave</span>";
}
else // If not..
{
    btn.innerHTML = "<span>Join</span>";
}
}

function checker()
{
window.alert("checker function");
if (checkEmail(this) = true)
  {
    window.alert("email correct");
    popupClick();
    return true;
  }
elseif
{
   window.alert("email check wrong");
   return false;
}
}

}); // jQuery End
</script>

<form action="<?php echo $website.$relative_string;?>" name="subscribe" onsubmit="return checker();" method="post">
  <div id="cell8" class="titlecell2"><h3>Email:</h3></div>
     <div id="cell9" class="inputcell2">
        <input type="text" class="inputfield2" name="email" value="Your Email..." id="email2" maxlength="255" onfocus="this.value='';">
     </div>
     <div id="cell10" class="textcell3">
       <input name="group" type="hidden" id="group[]" value="<?php echo $group; ?>">
       <input name="subscribe" id="sub" type="radio" value="true" checked>
       </span>Subscribe</p>
     </div>
     <div id="cell11" class="buttoncell">
        <button type="submit" name="Submit2" value="Join" id="submitButton2" button" onClick="javascript:myFunction();"/>
        <span>Join</span>
        </button>
     </div>
     <div id="cell8" class="textcell4">
       <input type="radio" name="subscribe" id="unsub" value="false">
       </span>Un-Subscribe</p>
     </div>
</form>

1 个答案:

答案 0 :(得分:0)

您的代码存在很多问题。修复所有这些,然后再试一次:

  • onClick="javascript:myFunction();"错了。引用的字符串必须是Javascript代码,因此javascript:不属于那里。您将此与<a href="javascript:...混淆。您想要onclick="myFunction()"

  • 您有自动关闭按钮标记:<button ... />Join</button>其中/>会立即关闭标记,因此它不会包含后面的“加入”文本。这意味着如果您单击“加入”,它将不与该按钮关联,因此不会发生任何事情。您的编辑应该将其标记为错误。

  • </span>Subscribe</p>之前没有打开任何跨度或段落。您需要使用可以突出显示的编辑器。如果Visual Studio过度,我建议使用Microsoft的免费Expression Web。 (我经常使用它。)

  • 在checker()函数中,if条件包含=赋值运算符。您需要使用==比较运算符。

  • 您无需在条件中使用== true。而不是if ( blah == true )只使用if ( blah )。在某些高级案例中,您可能希望使用=== true ===表示“与...相同”,但此处不适用。

  • 在您的checker()函数中,elseif应该只是else,因为您没有测试其他条件。

  • 在checker()函数中,checkEmail(this)将无效,因为没有将checker()作为对象的方法调用,因此未定义thisonsubmit="return checker()"实际上定义了一个包含return checker()作为函数体的匿名方法函数。最简单的解决方法是将this作为参数传递给checker()函数,如下所示:

    function checker(theForm) {
        if ( checkEmail(theForm) ) {
            ...
    
    <form onsubmit="return checker(this)" ...>
    
相关问题