如何使用无线电按钮重定向到下一页,而无需提交按钮



此代码提供提交按钮

脚本

<script type="text/javascript">
function get_action(form) {
    form.action = document.querySelector('input[name = "x"]:checked').value;;
}

html表格

<form name = "quoted" method="get" onsubmit="get_action(this);">
 <input id = "poster" type="text" name="poster" required="required" placeholder = "Credited Individual.">     <br>
 <textarea class = "actual_quote" name = "actual_quote" required="required" placeholder = "Write the question here!"></textarea><br><br><br>
 <div class = "checkboxes" required="required">
     <h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
     <label for="x"><input type="radio" name="x" value="stupid.php" id = "x" checked="checked" />    <span>stupid</span></label><br>
     <label for="x"><input type="radio" name="x" value="stupider.php" id = "x" /> <span>stupider</span>    </label><br>
     <label for="x"><input type="radio" name="x" value="stupidest.php" id = "x"/>    <span>stupidest</span></label>
 </div>
 <input id = "submit1" type="submit"><br>

  1. 不要在class=""class = ""
  2. 之间混合您的代码风格
  3. div不能有必要的
  4. ID必须是唯一的
  5. 使用jQuery

$(document).ready(function(){
// debug output
    console.log('Document ready');
    $('#submit1').click(function(event){
	event.preventDefault();
	var redirect = $('input[name=x]:checked', '#quoted').val();
	alert (redirect);
	location.href = redirect;
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
	<form id="quoted">
		<input id="poster" type="text" name="poster" required="required" placeholder="Credited Individual."><br>
		<textarea class="actual_quote" name="actual_quote" required="required" placeholder="Write the question here!"></textarea><br><br><br>
		<div class = "checkboxes" required="required">
			<h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
			<label for="x"><input type="radio" name="x" value="stupid.php" checked="checked" />    <span>stupid</span></label><br>
			<label for="x"><input type="radio" name="x" value="stupider.php"/> <span>stupider</span>    </label><br>
			<label for="x"><input type="radio" name="x" value="stupidest.php"/>    <span>stupidest</span></label>
		</div>
		<input id="submit1" type="button"><br>
	</form>
  </body>

最新更新