-2

Webサイトに2つのラジオボタンを含む単純なフォームがあり、どちらのラジオボタンもクリックされなかった場合のJavascriptフォーム検証を追加しました。(SOに感謝)ただし、ユーザーが[OK]ボタンをクリックすると、ページはフォームの「プロセス」ページにリダイレクトされます。ユーザーがアラートボックスの[OK]ボタンをクリックしたときに、ページをリロードするか、アラートボックスを閉じるという動作が望ましいです。

私が話していることをさらに確認するには、chrisrjones.comにアクセスして、chrisrjones.com/splash.phpにリダイレクトしてからEnterをクリックします。(ラジオボタンを選択せず​​に)アラートボックスが表示され、[ OK ]ボタンが押されると、ページはフォームプロセスページにリダイレクトされます(私が望むものではありません)。

<!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 に答える