-2

我在一个包含两个单选按钮的网站上有一个简单的表单,并且我添加了一些 Javascript 表单验证,用于在没有单击这两个单选按钮时进行。(谢谢)但是,当用户单击确定按钮时,页面将重定向到表单的“处理”页面。当用户单击警报框中的“确定”按钮时,我更喜欢仅重新加载页面或关闭警报框的行为。

要进一步了解我在说什么,您可以访问 chrisrjones.com,它应该会将您重定向到 chrisrjones.com/splash.php,然后单击 Enter。(不选择单选按钮)应该会出现一个警告框,一旦按下确定按钮,页面就会重定向到表单处理页面(不是我想要的)。

<!DOCTYPE html>

<html lang="en">

<head>
<title>chrisRjones.com - Splash</title>

 <?php
function getImagesFromDir($path) {
    $images = array();
    if ( $img_dir = @opendir($path) ) {
        while ( false !== ($img_file = readdir($img_dir)) ) {
            // checks for gif, jpg, png
            if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
                $images[] = $img_file;
            }
        }
        closedir($img_dir);
    }
    return $images;
}

function getRandomFromArray($ar) {
    mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
    $num = array_rand($ar);
    return $ar[$num];
}

/////////////////////////////////////////////////////////////////////
// This is the only portion of the code you may need to change.
// Indicate the location of your images 

$root = '';
// use if specifying path from root
//$root = $_SERVER['DOCUMENT_ROOT'];

$path = 'pics/';

// End of user modified section 
/////////////////////////////////////////////////////////////////////

// Obtain list of images from directory 
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?> 





<!-- begin google analytics PHP include -->

<?php include_once("analyticstracking.php") ?>

<!-- end google analytics PHP include -->

<!-- Javascript radiobutton form validation -->

<script type="text/javascript">
function valForm(form) {
    var btn1 = parseInt(form.splash.value, 10) || 0;
    btn = (btn1 )
    if (btn < 3) {
        alert('You either like splash pages or hate em, choose one ...please');
        return false; }
    else {
     alert('Button value ' + btn + ' selected');
    }
}
</script>

<!-- Javascript radiobutton form validation END -->


</head>

<body>
<img src="<?php echo $path . $img ?>" alt="" width="640" height="auto"  />
<h1>Hello Javascript redirection.</h1><br />
<p>

<form name="tosplashornottosplash" action="splash-process.php" method="post">
Splash pages are stupid 

<input type="radio" name="splash" value="false" /> No
<input type="radio" name="splash" value="true" /> Yes

<input type="submit" name="formSubmit" onClick="valForm(tosplashornottosplash)" value="Enter" />
</form>



</body>
</html>
4

3 回答 3

2

在您想留在同一页面上的情况下,从 valform 返回 false。

例如:

function valForm(form) {
var btn1 = parseInt(form.splash.value, 10) || 0;
btn = (btn1 )
if (btn < 3) {
   alert('You either like splash pages or hate em, choose one ...please');
   return false;
}
else {
     alert('Button value ' + btn + ' selected');
} 
}

您可能还需要更改您的标记,如下添加return语句。

<input type="submit" value="Enter" onclick="return valForm(tosplashornottosplash)" name="formSubmit">
于 2013-01-12T00:03:19.040 回答
1

你只需要return false在那里做一个防止默认操作。

function valForm(form) {
    var btn1 = parseInt(form.splash.value, 10) || 0;
    btn = (btn1 )
    if (btn < 3) { 
        alert('You either like splash pages or hate em, choose one ...please');
        return false;
    } else {
        alert('Button value ' + btn + ' selected');
    }
}
于 2013-01-12T00:04:22.373 回答
-1

你可以使用 JQuery。

function valForm(){
  if (!$("input[name=splash]:selected").length)
    alert('please select');
    return false;
  } else { return true; }
}
于 2013-01-12T00:07:03.380 回答