簡體   English   中英

在php循環中傳遞javascript動作

[英]passing javascript actions within php while loop

我需要以下功能:

“選中復選框后,必須突出顯示相應的文本框,用戶才能輸入一些文本...”

復選框正在工作,但是當選中復選框時,相應的文本框未激活,如何解決此問題?

這是我的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>

嘗試這個:

正如您所說,下面的代碼具有在選中復選框時啟用文本框的功能,並在取消選中時再次禁用它。

這是一個工作示例:

<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);
    }
}
?>

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

而不是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

使用這個例子

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM