在表单提交后阻止模式重新加载

时间:2018-01-15 19:20:00

标签: javascript jquery bootstrap-modal

我正在尝试构建一个具有在页面加载时打开的模式的工具,然后用户将文本粘贴到textarea并提交。之后,应该运行一个函数,该函数返回该文本的子字符串,用于特定数量的字符。

目前我是控制台记录正确的子字符串,但在我单击提交按钮后,模式再次运行。我只希望模态在页面加载时运行一次。

有人可以提供一些指导吗?



$(window).on('load',function() {
	$('#myModal').modal('show');
	
	
});

function myFunction(info) {
	var info = $('#textArea').val();
  var result = info.substring(0, 66);
	
	
	$('#result').html(result);
	console.log(result);
	
}

.container >* {
	margin:20px auto;
	width: 80%;
}

<head>
	<title>Bootstrap Example</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
</head>

<body>

	<!-- The Modal -->
	<div class="modal fade" id="myModal">
		<div class="modal-dialog">
			<div class="modal-content">

				<!-- Modal Header -->
				<div class="modal-header">
					<h4 class="modal-title">Welcome to my Bringhurst Rule Tool</h4>
					<button type="button" class="close" data-dismiss="modal">&times;</button>
				</div>

				<!-- Modal body -->
				<div class="modal-body">
					<p>Bringhurst introduces designers to the "66 character rule," which states that 66 characters including special characters and spaces at a font-size of 16px is the ideal width for a single line of type in order to achieve maximum clarity and readability.</p>
					<p>Close the modal to see what your 66 characters look like.</p>
				</div>

				<!-- Modal footer -->
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
				</div> <!-- footer end -->
			</div> <!-- modal-content end -->
		</div> <!-- modal dialog end -->
	</div> <!-- mymodal end -->
	
	<div class="container">
		<header><h1>Bringhurst Rule</h1></header>
		<form class="align-center">
			<div class="form-group">
				<label for="textArea"></label>
				<textarea class="form-control" id="textArea" rows="3" placeholder="Paste your text here..."></textarea>
			</div>
			<button type="submit" class="btn btn-info" onclick="myFunction()">Get the substring!</button>
		</form>
		
		
	</div>
	<div id="result"></div>
	
</body>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

有更好的方法可以做你正在做的事情,比如使用jQuery的花哨事件处理程序而不是使用内联事件处理程序,但对你的问题最简单的答案是在事件上返回false ...通过进行两次更改来做到这一点..

  1. 通过更改HTML来使事件侦听器返回事件处理程序的结果。onclick="return myFunction()"(在那里添加return
  2. 来自函数本身的
  3. return false
  4. 通过返回false,您将阻止默认操作,即提交表单。

    或者,您可以删除表单本身或将提交按钮更改为常规按钮。

    $(window).on('load',function() {
    	$('#myModal').modal('show');
    	
    	
    });
    
    function myFunction(info) {
    	var info = $('#textArea').val();
      var result = info.substring(0, 66);
    	
    	
    	$('#result').html(result);
    	console.log(result);
    	return false;
    }
    .container >* {
    	margin:20px auto;
    	width: 80%;
    }
    <head>
    	<title>Bootstrap Example</title>
    	<meta charset="utf-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1">
    	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
    	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
    	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
    </head>
    
    <body>
    
    	<!-- The Modal -->
    	<div class="modal fade" id="myModal">
    		<div class="modal-dialog">
    			<div class="modal-content">
    
    				<!-- Modal Header -->
    				<div class="modal-header">
    					<h4 class="modal-title">Welcome to my Bringhurst Rule Tool</h4>
    					<button type="button" class="close" data-dismiss="modal">&times;</button>
    				</div>
    
    				<!-- Modal body -->
    				<div class="modal-body">
    					<p>Bringhurst introduces designers to the "66 character rule," which states that 66 characters including special characters and spaces at a font-size of 16px is the ideal width for a single line of type in order to achieve maximum clarity and readability.</p>
    					<p>Close the modal to see what your 66 characters look like.</p>
    				</div>
    
    				<!-- Modal footer -->
    				<div class="modal-footer">
    					<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    				</div> <!-- footer end -->
    			</div> <!-- modal-content end -->
    		</div> <!-- modal dialog end -->
    	</div> <!-- mymodal end -->
    	
    	<div class="container">
    		<header><h1>Bringhurst Rule</h1></header>
    		<form class="align-center">
    			<div class="form-group">
    				<label for="textArea"></label>
    				<textarea class="form-control" id="textArea" rows="3" placeholder="Paste your text here..."></textarea>
    			</div>
    			<button type="submit" class="btn btn-info" onclick="return myFunction()">Get the substring!</button>
    		</form>
    		
    		
    	</div>
    	<div id="result"></div>
    	
    </body>

答案 1 :(得分:0)

您应该阻止在子网站后重新加载页面,并且可以返回false onclick个事件;

<button type="submit" class="btn btn-info" onclick="myFunction(); return false;">Get the substring!</button>

$(window).on('load',function() {
	$('#myModal').modal('show');
	
	
});
function myFunction() {
  var info = $('#textArea').val();
  var result = info.substring(0, 66);
  $('#result').html(result);
  console.log(result);
}
.container >* {
	margin:20px auto;
	width: 80%;
}
<title>Bootstrap Example</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<body>

	<!-- The Modal -->
	<div class="modal fade" id="myModal">
		<div class="modal-dialog">
			<div class="modal-content">

				<!-- Modal Header -->
				<div class="modal-header">
					<h4 class="modal-title">Welcome to my Bringhurst Rule Tool</h4>
					<button type="button" class="close" data-dismiss="modal">&times;</button>
				</div>

				<!-- Modal body -->
				<div class="modal-body">
					<p>Bringhurst introduces designers to the "66 character rule," which states that 66 characters including special characters and spaces at a font-size of 16px is the ideal width for a single line of type in order to achieve maximum clarity and readability.</p>
					<p>Close the modal to see what your 66 characters look like.</p>
				</div>

				<!-- Modal footer -->
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
				</div> <!-- footer end -->
			</div> <!-- modal-content end -->
		</div> <!-- modal dialog end -->
	</div> <!-- mymodal end -->
	
	<div class="container">
		<header><h1>Bringhurst Rule</h1></header>
		<form class="align-center">
			<div class="form-group">
				<label for="textArea"></label>
				<textarea class="form-control" id="textArea" rows="3" placeholder="Paste your text here..."></textarea>
			</div>
			<button type="submit" class="btn btn-info" onclick="myFunction(); return false;">Get the substring!</button>
		</form>
		
		
	</div>
	<div id="result"></div>
	
</body>