问题
i have a form with 2 radio buttons and a submit button. Both radio buttons have url of different pages, like one will open google.com and another will open yahoo.com. have selected a radio button and submit the page. i want it to open my url. Please help. with HTML and javascript code. Thanks in advance.
回答1:
Do something like this
<?
if ($_POST['submitUrl']){
header('Location:'.$_POST['url']);
}
?>
<form method="post" action="">
<input type="radio" name="url" value="http://www.google.com"/>Link1<br>
<input type="radio" name="url" value="http://www.yahoo.com"/>Link2<br>
<input type="submit" name"submitUrl" value="Submit"/>
</form>
回答2:
HTML
<form name="form" method="post" action="">
<input type="radio" name="url" value="http://www.google.com" /> Google
<input type="radio" name="url" value="http://www.yahoo.com" /> Yahoo
<input type="submit" name="submit" value="Submit" />
</form>
JQuery
$(function(){
$('form').submit(function(event){
event.preventDefault();
window.location = $('input[type=radio]:checked').val();
});
});
Working example: http://jsfiddle.net/AlienWebguy/Rpzvd/3/
回答3:
Save yourself 90k of extra script:
<script type="text/javascript">
function doSubmit(form) {
var urls = form['url'];
var i = urls && urls.length;
while (i--) {
if (urls[i].checked) {
window.location = urls[i].value;
}
}
return false;
}
</script>
<form action="" onsubmit="return doSubmit(this)">
<input type="radio" name="url" value="http://www.google.com"> Google
<input type="radio" name="url" value="http://www.yahoo.com"> Yahoo
<input type="submit" value="Submit">
</form>
Oh, and never give a form control a name of "submit" as it will mask the form's submit method. If you must, name it "submitButton" or similar.
来源:https://stackoverflow.com/questions/6741340/radio-button-to-open-pages