1. 程式人生 > >form提交action之前用js操作

form提交action之前用js操作

在提交action之前先用js操作一下,若果為false則不執行action:

html:

<form name="launch" method="post" onsubmit="return launchTopic();" action="/sns/launch.html">
	<textarea class="txd-comment" rows="5" name="content" id="launchTopicContent"></textarea>
	<input type="hidden" name="circle" value="${id}">
	<div class="row"><span class="textbutton"><input type="submit" name="submit_btn" value="發起話題"></span></div>
</form>

js:
                     <script type="text/javascript">
	        	function launchTopic(){
	        		if(!isLogin()){
	        			return false;
	        		}
	        		var content = document.getElementById('launchTopicContent').value;
	        		if(content == null || content.trim().length == 0){
	        			alert("請輸入內容");
	        			return false;
	        		}
	        		return true;
	             }