开发者

javascript undefined error from form input radio button

I have a pop up window which lists images using a form and radio buttons.

When i click a radio button, the function is called which should pass back to the parent window.

I keep getting 'undefined' as the value which means to me that the variable hasn't been set.

How do i get around this error?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!-- Begin
function sendValue (s){
var selvalue = s.value;
//window.opener.document.getElementById('details').value = selvalue;
//window.close();

alert(selvalue);
}
//  End -->
</script>
<form name="selectform">

<?php

    // Define the full path to your folder from root

    $path = "../../images/news/";

    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");

    // Loop through the files
    while ($file = readdir($dir_handle)) {
        if($file == "." || $file == ".." || $file == "index.php" )
            continue;
        $myfile = $file;
        echo '<input type="radio" name="details" value="'.$myfil开发者_StackOverflowe.'" onClick="sendValue(this.form.details);" />'.$file.'<br />';
    }

    // Close
    closedir($dir_handle);
?>
</form>
</body>
</html>


Why not just send the radio button value directly to the function, rather than the full radio button reference:

HTML

<input type="radio" name="details" value="1" onClick="sendValue(this.value);" />
<input type="radio" name="details" value="2" onClick="sendValue(this.value);" />

Javascript

function sendValue(val){
   alert(val);  
   if(window.opener && !window.opener.closed) {
      // do something with the parent window
   }
}

In action here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜