在php循环中传递javascript动作

时间:2012-12-25 07:16:24

标签: php javascript

我需要以下功能:

“选中复选框后,必须突出显示相应的文本框,用户可以在其中输入一些文字..”

复选框正常工作,但选中复选框后,相应的文本框未激活,如何解决?

这是我的php代码:

<?php
if ($handle = opendir('/year_no/albums')) {
    $blacklist = array('.', '..');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
            echo "<form name='form1'>
            <div class='controlset-pad'>
            <font color='black'><label><input type='checkbox' name='album[]' value='$album' onclick='enable_text(this.checked)' class='medium'/><span>$album</span></label><br/></div>​";
            echo"<script> 
            function enable_text(status)
            {
                status=!status;
                document.form1.other_name.disabled = status;
            }
            </script>";
            echo "<div class='field4'><label>Add your comment here</label><input type='text' name='other_name' class='medium' disabled='disabled'/></div></form>";
        }
    }
    closedir($handle);
}
?>

修改后的代码:

<script> 
function enable_text(status, sub)
{
status=!status;
document.getElementById(sub).disabled = status;
}
</script>
<form name='form1'>
<?php
if ($handle = opendir('/year_no/albums')) {
    $blacklist = array('.', '..');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
echo "<div class='controlset-pad'>
<font color='black'><label><input type='checkbox' name='file[]' value='$file' onclick='enable_text(this.checked, $file)' class='medium'/><span>$file</span></label>
</div>";
echo "<div class='field4'><label>Add your comment here</label><input type='text' name='sub' id='$file' class='medium' disabled='disabled'/></div>";
        }
    }
    closedir($handle);
}
?>
</form>

3 个答案:

答案 0 :(得分:6)

试试这个:

正如您所说,下面的代码具有在选中复选框时启用文本框的功能,并在未选中时再次禁用它。

这是一个有效的Example:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js">    </script>
<!-- include JQuery using above line -->

<!-- Here is your small javascript -->
<script type="text/javascript">
$(document).ready(function(e) {
    $("input[type=checkbox]").on("click",function(){
        if($(this).is(':checked')){
                    //If checkbox is checked do whatever you want to do here.
            $("#txt_"+$(this).attr("id")).removeAttr("disabled");
                    $("#drpdwn_"+$(this).attr("id")).removeAttr("disabled");
        } else {
                   // If unchecked.
            $("#txt_"+$(this).attr("id")).attr("disabled","disabled");
                    $("#drpdwn_"+$(this).attr("id")).attr("disabled","disabled");
        }
    });
});
</script>
<?php
$dir = "/year_no/albums";

if(is_dir($dir)) {
    if ($dh = opendir($dir)) {
        $blacklist = array('.', '..');
        $count=1;
        while (($file = readdir($dh)) !== false) {
            if (!in_array($file, $blacklist)){  
                echo "<div class='controlset-pad'>
                        <font color='black'>
                            <label>
                                <input type='checkbox' name='file[]' id='".$count."' value='enabletxtbox' class='medium'/>
                                <span>".$file."</span>
                            </label>
                    </div>";
                echo "<div class='field4'>
                        <label>Add your comment here</label>
                        <input type='text' name='sub' id='txt_".$count."' class='medium' disabled='disabled'/>";

                if(filetype($dir.$file) == "dir"){
                        $dir2 = $dir.$file;
                        $dh2=opendir($dir2);
                        $drpdwn="<select id='drpdwn_".$count."' disabled='disabled'>";
                        while(($file2 = readdir($dh2)) !== false){
                            if(!in_array($file2, $blacklist)){
                                $drpdwn.='<option>'.$file2.'</option>';
                            }
                        }
                        $drpdwn.="</select>";
                        closedir($dh2);
                        echo $drpdwn;

                }
                echo"</div>";
            }
            $count++;
        }
        closedir($dh);
    }
}
?>

答案 1 :(得分:2)

您的代码非常完美,只需添加一个小的更改即可。

而不是id

document.getElementById('other_name').disabled = status;

请注意,id必须对每个元素都是唯一的。

编辑使用此代码

<?php
if ($handle = opendir('/year_no/albums')) {
    $blacklist = array('.', '..');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
echo "<form name='form1'>
<div class='controlset-pad'>
<font color='black'><label><input type='checkbox' name='album[]' value='$album' onclick='enable_text(this.checked, $album)' class='medium'/><span>$album</span></label><br/>
</div>?";
echo"<script> 
function enable_text(status, album)
{
status=!status;
document.getElementById(album).disabled = status;
}
</script>";
echo "<div class='field4'><label>Add your comment here</label><input type='text' name='other_name' id = '$album' class='medium' disabled='disabled'/></div></form>";
        }
    }
    closedir($handle);
}
?>

请注意,$ album值不能为null

答案 2 :(得分:2)

使用此示例

    <form name="form1">
    <script>
    function enable_text(status)
            {
            alert(status)
            status=!status;
            document.form1.other_name.disabled = status;
            }
    </script>
        <input type='checkbox' name='album' value='hi' onclick='enable_text(this.checked)' class='medium'/>
        <input type='text' name='other_name' class='medium' disabled='disabled'/>
    </form>