1

私はここで少し「歯が生える」問題を抱えていますこれまでの私のコードです

<form name="job_app">
    Source?<br/>
        <input type="radio" name="source" value="GAZ" id="GAZ" /> Stonoway Gazette <br/>
        <input type="radio" name="source" value="JCP" id="JCP" /> Job Center <br/>
        <input type="radio" name="source" value="WOM" id="WOM" /> Word of Mouth <br/><br/>

    <script language="text/JavaScript">
        if (document.job_app.source.GAZ.checked){
            document.write='Issue <br/><input type="text" name="issue" /><br/><br/>';
        }
        else if (document.job_app.source.JCP.checked){
            document.write='Ref <br/><input type="text" name="ref" /><br/><br/>';
        }
        //word of mouth has no additional input so there is no if statement for it
    </script>
</form>

私がこれをしたいのは、複数のテキストボックスを作成せずに、選択されているラジオボタンに応じて問題または参照テキストボックスを作成(または再表示)することです。

これが新人の間違いである場合はご不便をおかけして申し訳ありません。私はこれまでJavaやそのような言語を使用したことがありません。

これは、Amy McCrobieのおかげで、2012年5月26日07:15現在の作業コードです。エイミーのバージョン(以下を参照)以降、いくつかの編集が行われています。次のいくつかのフィールドを簡単に追加できるように、すべてのスクリプトをフォームの上に移動し、口コミのステートメントを追加しました。<head>これは、index.phpとmeta.phpの一部であるため省略されています。これはform.php用ですが、スペーサーを追加し、関数名をより具体的にしました。

index.php

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
    <head>
        <?php
            include './meta.php';
        ?>
    </head>
    <?php
        /*if(isset($_POST['submit'])){
            include './submit.php';
        }
        else{*/
            include './form.php';
        //}
    ?>
</html>

meta.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta http-Equiv="Cache-Control" Content="no-cache" />
<meta http-Equiv="Pragma" Content="no-cache" />
<meta http-Equiv="Expires" Content="0" />
<title>job_app</title>
<link rel="StyleSheet" type="text/css" href="./style.css"/>

form.php

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#issueEl").hide();
    $("#refEl").hide();
});
    function showHide_source(){
        if (document.getElementById('GAZ').checked)
        {
            document.getElementById('issueEl').style.display = 'block';
            document.getElementById('refEl').style.display = 'none';
            document.getElementById('src_spEl').style.display = 'none';
        } 
        if (document.getElementById('JCP').checked)
        {
            document.getElementById('issueEl').style.display = 'none';
            document.getElementById('refEl').style.display = 'block';
            document.getElementById('src_spEl').style.display = 'none';
        }
    if (document.getElementById('WOM').checked)
        {
            document.getElementById('issueEl').style.display = 'none';
            document.getElementById('refEl').style.display = 'none';
            document.getElementById('src_spEl').style.display = 'block';
        }
    }
</script>
<form name="job_app" action="" method="post"> 
    Source?<br/> 
        <input type="radio" name="source" value="GAZ" id="GAZ" onChange="showHide_source()" /> Stonoway Gazette <br/> 
        <input type="radio" name="source" value="JCP" id="JCP" onChange="showHide_source()" /> Job Center <br/> 
        <input type="radio" name="source" value="WOM" id="WOM" onChange="showHide_source()" /> Word of Mouth <br/><br/> 
    <div class="hideable" id="issueEl">Issue <br/><input type="text" name="issue" /><br/><br/></div>
    <div class="hideable" id="refEl">Ref <br/><input type="text" name="ref" /><br/><br/></div>
    <div class="hideable" id="src_spEl"></div>
    rest of form        
    <input...
     .../>
</form>

style.css

div.hideable{
    height: 62px;
}
4

2 に答える 2

2

これを試して:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-Equiv="Cache-Control" Content="no-cache">
    <meta http-Equiv="Pragma" Content="no-cache">
    <meta http-Equiv="Expires" Content="0">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#issueEl").hide();
            $("#refEl").hide();
        });
    </script>
    <title>Test</title>
</head>
<body>
<form name="job_app" action="" method="post"> 
    Source?<br/> 
        <input type="radio" name="source" value="GAZ" id="GAZ" onChange="showHide()" /> Stonoway Gazette <br/> 
        <input type="radio" name="source" value="JCP" id="JCP" onChange="showHide()" /> Job Center <br/> 
        <input type="radio" name="source" value="WOM" id="WOM" onChange="showHide()" /> Word of Mouth <br/><br/> 
        <div id="issueEl">Issue <br/><input type="text" name="issue" /><br/><br/></div>
        <div id="refEl">Issue <br/><input type="text" name="ref" /><br/><br/></div>
</form>
<script type="text/javascript">
function showHide(){
    if (document.getElementById('GAZ').checked)
    {
        document.getElementById('issueEl').style.display = 'block';
        document.getElementById('refEl').style.display = 'none';
    } 
    if (document.getElementById('JCP').checked)
    {
        document.getElementById('issueEl').style.display = 'none';
        document.getElementById('refEl').style.display = 'block';
    }
}
</script>
</body>
</html>
于 2012-05-25T20:28:56.133 に答える
0

私はjQueryを提案します、学ぶのは難しいことではありません、実際私は基本的なjavascriptよりもはるかに簡単だと思いますまた、非表示になっている2つのテキストボックスのルートに行き、jQueryを介してそれらを表示します

現在のところ、ユーザーが1つのラジオボタンをチェックすると、他の2つのテキストボックスが表示されると思います。

jQueryを追加すると(ページの上部にsriptを含めるのと同じくらい簡単ですが、コードは次のようになります...

<form name="job_app"> 
    Source?<br/> 
        <input id="GAZ" type="radio" name="source" value="GAZ" id="GAZ" /> Stonoway Gazette <br/> 
        <input id="JCP" type="radio" name="source" value="JCP" id="JCP" /> Job Center <br/> 
        <input id="WOM" type="radio" name="source" value="WOM" id="WOM" /> Word of Mouth <br/><br/> 
        Issue <br/><input id="issue" type="text" name="issue" /><br/><br/>
        Issue <br/><input id="ref" type="text" name="ref" /><br/><br/>

    <script language="text/JavaScript">

        $("#issue").css("display","none"); 
        $("#ref").css("display","none"); 

        if ($("#GAZ").checked){ 
            $("#issue").css("display","inline"); 
            $("#ref").css("display","none"); 

        } 
         if ($("#JCP").checked){ 
            $("#issue").css("display","none"); 
            $("#ref").css("display","inline"); 

        } 
        //word of mouth has no additional input so there is no if statement for it 
    </script> 
</form> 
于 2012-05-25T20:35:43.620 に答える