admin管理员组

文章数量:1345288

$(function() {
  function make_alert() {
    var text = $('#text_').val(); //let suppose it as true
    if (true) {
      alert("yes_text");
      return false;
    } else {
      alert("no_text");
      return false;
    }
  }
});
<script src=".3.1/jquery.min.js"></script>
<form method="post" action="#" onsubmit="return make_alert()">
	<textarea id="text_">
	</textarea>
	<button id="btn_submit" type="submit" name="type" value="create_first">
	Submit
	</button>
</form>

$(function() {
  function make_alert() {
    var text = $('#text_').val(); //let suppose it as true
    if (true) {
      alert("yes_text");
      return false;
    } else {
      alert("no_text");
      return false;
    }
  }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post" action="#" onsubmit="return make_alert()">
	<textarea id="text_">
	</textarea>
	<button id="btn_submit" type="submit" name="type" value="create_first">
	Submit
	</button>
</form>

This code is alerting when I click submit button.

I write onsubmit in the html form tag, but It seems not working.

How can I do this?

What I want to is that I can treat some stuff before form post. So I think I need those functions.

Share Improve this question edited Jun 7, 2018 at 10:38 Akhil Aravind 6,1301 gold badge17 silver badges37 bronze badges asked Jun 7, 2018 at 10:04 David Foie GrasDavid Foie Gras 2,0804 gold badges25 silver badges44 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

The make_alert function is not "global" so it can't be found.

Here's one solution

  var make_alert = (function() {
    var text = $('#text_').val(); //let suppose it as true
    if (true) {
      alert("yes_text");
      return false;
    } else {
      alert("no_text");
      return false;
    }
  });
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form method="post" action="#" onsubmit="return make_alert()">
  <textarea id="text_">
  </textarea>
  <button id="btn_submit" type="submit" name="type" value="create_first">
                        Submit
                    </button>

</form>

You can do it like this. Remove function from $(function() {

https://codepen.io/creativedev/pen/eKdrdK

function make_alert() {
    var text = $('#text_').val(); //let suppose it as true
    if (true) {
      alert("yes_text");
      return false;
    } else {
      alert("no_text");
      return false;
    }
  }

for me, it was because I put the required attribute which prevents onsubmit=""

本文标签: javascriptltform onsubmitquotfunction()quotgt is not workingStack Overflow