从表单输入单选按钮javascript undefined错误

时间:2010-08-05 18:04:08

标签: javascript forms

我有一个弹出窗口,使用表单和单选按钮列出图像。

单击单选按钮时,将调用该函数,该函数应传回父窗口。

我继续将'undefined'作为对我来说意味着尚未设置的值。

我如何解决此错误?

<!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="'.$myfile.'" onClick="sendValue(this.form.details);" />'.$file.'<br />';
    }

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

1 个答案:

答案 0 :(得分:1)

为什么不直接将单选按钮值发送到函数,而不是完整的单选按钮引用:

<强> 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
   }
}

action here