简体   繁体   English

为什么没有出现警报消息?

[英]Why the alert message is not appearing?

I've a following HTML code: 我有以下HTML代码:

<form name="question_issue_form" id="question_issue_form" action="question_issue.php">
      <table class="trnsction_details" width="100%" cellpadding="5">
        <tbody>    
          <tr>
            <td></td>
            <td>
              <input type="checkbox" name = "que_issue[]" value = "Question is wrong" id ="chkQueWrong">Question is wrong</input>
            </td>
          </tr>
          <tr>
            <td></td>
            <td><input type="checkbox" name = "que_issue[]" value = "Answers are wrong" id ="chkAnsWrong">Answers are wrong</input></td> 
          </tr>
          <tr>
            <td></td>
            <td><input type="checkbox" name = "que_issue[]" value = "Question direction is incorrect" id ="chkDirIncorrect">Question direction is incorrecct</input></td>                
          </tr>
          <tr>
            <td></td>
            <td><input type="checkbox" name = "que_issue[]" value = "Other" id ="chkOther">Other</input></td>          
          </tr>
          <tr>
            <td></td>
            <td class="set_message" style="display:none;"><textarea name="que_issue_comment" rows="4" cols="25" maxlength="100"></textarea></td>      
          </tr>
          <tr>
            <td></td>
            <td><input type="submit" name="submit" value="Submit" id="report_question_issue"></td>
          </tr>
        </tbody>
      </table>
    </form>

Following is my JS code: 以下是我的JS代码:

$(document).on('click', '#report_question_issue', function (e) {
    alert("Jumbo man");
}

And upon clicking the submit button I'm calling the alert to appear but it's not appearing. 在单击“提交”按钮后,我将调用警报,但它没有出现。 I'm not getting why this is happening? 我不明白为什么会这样吗? For your refrence following is link to the js Fiddle; 为了您的方便,以下是js小提琴的链接; http://jsfiddle.net/TjK2N/ http://jsfiddle.net/TjK2N/

You are not including JQuery Reference. 您不包括JQuery参考。 And you are missing to close ); 而且您缺少关闭);

DEMO 演示

In your code bracket isn't closed properly at last. 最后,您的代码支架未正确关闭。 this is required ")" see this updated fiddle 这是必需的“)”, 请参阅此更新的小提琴

$(document).on('click', '#report_question_issue', function (e) {
    alert("Jumbo man");
});
$(document).on('click', '#report_question_issue', function (e) {
    alert("Jumbo man");
}

$(XXX) this is a jquery syntax, but i saw you are in the Pure JS environment. $(XXX)这是一种jQuery语法,但是我看到您在Pure JS环境中。 Should you use a js library? 您应该使用js库吗?

The problem is with the submit action of the <form> that reload the page before the JS is called. 问题在于<form>submit操作,该操作在调用JS之前重新加载页面。
To avoid it, you can change it by one of following : 为避免这种情况,可以通过以下方法之一进行更改:

1st using JS into action attribute 1使用JS到action属性

<form name="question_issue_form" id="question_issue_form" action="javascript:alert('test')">

2nd using onsubmit attribute 第二次使用onsubmit属性

<form name="question_issue_form" id="question_issue_form" onsubmit="alert('test');" action="question_issue.php">

3rd using jQuery on submit event 第三在submit事件上使用jQuery

$('form#question_issue_form').submit(function() {
    alert('test');
    return false; //to avoid the reload
});

4th using jQuery on click event on the submit button 第4次在提交按钮上的click事件上使用jQuery

$('form#question_issue_form').click(function() {
    alert('test');
    return false; //to avoid the reload
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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