代码之家  ›  专栏  ›  技术社区  ›  AdRock

表单输入单选按钮中的javascript未定义错误

  •  2
  • AdRock  · 技术社区  · 14 年前

    我有一个弹出窗口,其中列出了使用窗体和单选按钮的图像。

    当我单击一个单选按钮时,将调用该函数,该函数应返回到父窗口。

    我总是得到“未定义”作为值,这意味着我的变量还没有设置。

    我该如何避免这个错误?

    <!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 回复  |  直到 14 年前
        1
  •  1
  •   Pat    14 年前

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

    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 .