1

Stackは初めてで、これが私の最初の質問ですが、検索を行ったところ、具体的に達成しようとしていることを見つけることができませんでした。私はすべて学習しているので、これにアプローチする方法をよりよく理解するのに役立つ参考資料をいただければ幸いですが、誰かがサンプルコードを提供したい場合は、どちらでもかまいません:p

OK、シナリオはこれです。フォームに入力したいくつかのフィールドに基づいて自動クライアントノートを生成する「ノートクリエーター」を書いています。テキストフィールドから値を取得するスクリプトをすでに作成しましたが、ラジオオプションまたはテキストフィールドの組み合わせにしたいフィールドが1つあり、Javaは使用されたものと適切な値を取得する必要があります。どんな助けでもありがたいです、以下は私のコードです:

    <script type="text/javascript">
    function getNotes() {
        //Grab Variables
        spokeTo = document.thisForm.spokeWith.value;
        problemItem = document.thisForm.problemWith.value;
        resolvedBy = document.thisForm.resolvedWith.value;
        thisTech = document.thisForm.techName.value;
        fSpace = "... "
        //Read in the location information and prep the output container
        outputValue = "";
        {
        //Test if things are blank
        if (spokeTo == "") { alert('Please Enter the Name of the Person.'); }
        else if (problemItem == "") { alert('Please Select the Problem.');  }
        else if (resolvedBy == "") { alert('Please Type Your Resolution.'); }
        else {
            //The loop that puts the output together
                outputValue += "Spoke With: " + spokeTo + "\n\n" + "Called About: " + problemItem + "\n\n" + "Resolution: " + resolvedBy +fSpace + thisTech;
            }
            //output to the user
            document.thisForm.outputArea.value = outputValue;
        }
    }
</script>

        <form name="thisForm">
        <p>
        1. Spoke With: <input type="text" id="spokeWith" class="input" name="spokeWith">
        </p>

        <p>
        2. Called About: 
            Hardware <input type="radio" id="problemWith" class="input" name="problemWith" value="Hardware">
            Software <input type="radio" id="problemWith" class="input" name="problemWith" value="Software">
            <input type="text" id="problemWith" class="input" name="problemWith"><br>
        </p>

        <p>
        3. Resolved By:<br /><br />
            <textarea name="resolvedWith" id="resolvedWith"></textarea>
        </p>

        <p>
        4. Your Name:<br>
            <input type="text" id="techName" class="input" name="techName" /><br>
        </p>

        <p>
            <input type="button" class="button" value="Make My Notes!" onClick="javascript:getNotes();" />
            <input type="reset" class="button" value="Start Over" />
        </p>

        <br><br>

        <textarea name="outputArea" id="outputArea"></textarea>
        <p class="finishMessage" id="finishMessage" name="finishMessage"></p>
    </form>

特に、フォームのステップ2セクションを参照しています。このセクションには、ラジオオプションと、同じIDと名前のテキストフィールドがあります。IDはページごとに1回だけ使用されることになっているので、これはおそらく変更されるでしょうが、私はどんな提案/支援にもオープンです。私はJavascriptを使いこなしており、まだ学習段階にあります。

再度、感謝します!

- - - - - - - - ラウンド2 - - - - - - - -

これは、提供された回答の提案を受けた後の私の改訂されたコードです。スクリプトのどの部分をヒットするかを途中で知らせるために、一連のアラートを追加しました。最初のアラートを超えてトリガーするものを取得できず、出力をまったく取得できません。もう。私は何が欠けていますか?

<script type="text/javascript">
    function getNotes() {
    alert('You\'ve hit the function. Congratulations - you didn\'t break the whole damn thing.');
        //Grab Variables
        var spokeTo = document.thisForm.spokeWith.value;
        var resolvedBy = document.thisForm.resolvedWith.value;
        var thisTech = document.thisForm.techName.value;
        var fSpace = "&hellip; ";

        //Grab if radio else text
        var inputVal1 = document.getElementByClass('problemRadio').value;
        var inputVal2 = document.getElementByClass('problemWith').value;
        alert('You made it! Almost there.');
        // If Input Value 1 is not Null, Show Radio Value. Else, show Text Value
        var problemOutput;
        if (inputVal1.length > 0) {
        problemOutput = inputVal1;
        alert('I found value 1!');
        }
        else {
        problemOutput = inputVal2; 
        alert('I found value 2!'); 
        }

        //Read in the location information and prep the output container
        outputValue = "";

        //Test if things are blank
        if (spokeTo == "") { alert('Please Enter the Name of the Person.'); }
        else {
        alert('We Made it to Else to parse outputValue');
            //The loop that puts the output together
                outputValue += "Spoke With: " + spokeTo + "\n\n" + "Called About: " + problemOutput + "\n\n" + "Resolution: " + resolvedBy +fSpace + thisTech;
            }
            //output to the user
            document.thisForm.outputArea.value = outputValue;

    }
</script>

        <form name="thisForm">
        <p>1. Spoke With: <input type="text" id="spokeWith" class="input" name="spokeWith"></p>

        <p>2. Called About: 
            Hardware <input type="radio" id="problemRadio" class="problemRadio" name="problemRadio" value="Hardware">
            Software <input type="radio" id="problemRadio" class="problemRadio" name="problemRadio" value="Software">
            <input type="text" id="problemWith" class="problemWith" name="problemWith"><br>
        </p>

        <p>3. Resolved By:<br /><br />
            <textarea name="resolvedWith" id="resolvedWith"></textarea>
        </p>

        <p>4. Your Name:<br>
            <input type="text" id="techName" class="input" name="techName" /><br>
        </p>

        <p>
            <input type="button" class="button" value="Make My Notes!" onClick="javascript:getNotes();" />
            <input type="reset" class="button" value="Start Over" />
        </p>

        <br><br>

        <textarea name="outputArea" id="outputArea"></textarea>
        <p class="finishMessage" id="finishMessage" name="finishMessage"></p>
    </form>
4

2 に答える 2

0

1つのアプローチは、値をチェックすることです。

 var input1val = document.getElementById('input1Id').value;
 var input2val = document.getElementById('input2Id').value;

 var valfromeitherbox = (input1val.length > 0) ? input1val : input2val;

 /* UPDATE : ^ is the same as - 

 var valfromeitherbox;
 if(input1val.length > 0) { valfromeitherbox=input1val;
 } else { valfromeitherbox=input2val; }

 */

  document.getElementById('input3Id').value = valfromeitherbox;

 /* if val is empty in input1 use val from text box 2 */

少しごちゃごちゃしている場合は申し訳ありませんが、要点を理解していただければ幸いです。要点を見逃していません。

于 2012-05-16T19:24:55.063 に答える
0

申し訳ありませんが、ここにコードを取り込むための最良の方法はまだわかりません。いつも散らかってしまいます(私が理解するまで)

あなたのアップデートで私が見つけたものはほとんどありません。

1)getElementbyIdが最適です(最初の行を超えていませんでした)

2)ラジオボタンのIDが重複している

3)どのラジオボタンがチェックされたかを「チェック」する必要がありました

4)変数に警告するのに常に便利です(表示するためにいくつか追加しました)

私はこのコードを大まかなテストの下に与えました、そしてそれは線に沿っているように見えます..

以下のコードに追加されたコメントを参照してください。

 <script type="text/javascript">
   function getNotes() {

    //Grab Variables
    var spokeTo = document.getElementById('spokeWith').value;
    var resolvedBy = document.getElementById('resolvedWith').value;
    var thisTech = document.getElementById('techName').value;
    var fSpace = "&hellip; ";

    alert("spokeTo:" + spokeTo 
      +" , resolvedBy: " +resolvedBy+", thisTech: "+thisTech);

    //Grab if radio else text

    var problemWith = document.getElementById('problemWith').value;

   alert("problemWith: "+problemWith);

/* set a default value */
var problemOutput="other";

/* if they added no notes */
if((problemWith=="") || ( problemWith==null)) {

/* check which radio is selected if any */
if(document.getElementById('problemHardware').checked) {
problemOutput = document.getElementById('problemHardware').value;
}

if(document.getElementById('problemSoftware').checked) {
problemOutput = document.getElementById('problemSoftware').value;
}

} else {
problemOutput=problemWith;
}
alert("problem output is: "+problemOutput);

    alert('You made it! Almost there.');


    //Read in the location information and prep the output container
    outputValue = "";

    //Test if things are blank
    if (spokeTo == "") { alert('Please Enter the Name of the Person.'); }
    else {
    alert('We Made it to Else to parse outputValue');
    /* The loop that puts the output together */

    outputValue += "Spoke With: " 
     + spokeTo + "\n\n" 
     + "Called About: " 
     + problemOutput + "\n\n" 
     + "Resolution: " + resolvedBy +fSpace + thisTech;
        }
        //output to the user
        document.thisForm.outputArea.value = outputValue;

}
  </script>

      <form name="thisForm">
    <p>1. Spoke With: <input type="text" id="spokeWith" class="input" name="spokeWith">  </p>

    <p>2. Called About: 
        Hardware <input type="radio" id="problemHardware" class="problemRadio" name="problemRadio" value="Hardware">
        Software <input type="radio" id="problemSoftware" class="problemRadio" name="problemRadio" value="Software">
        <input type="text" id="problemWith" class="problemWith" name="problemWith"><br>
    </p>

    <p>3. Resolved By:<br /><br />
        <textarea name="resolvedWith" id="resolvedWith"></textarea>
    </p>

    <p>4. Your Name:<br>
        <input type="text" id="techName" class="input" name="techName" /><br>
    </p>

    <p>
        <input type="button" class="button" value="Make My Notes!" onClick="javascript:getNotes();" />
        <input type="reset" class="button" value="Start Over" />
    </p>

    <br><br>

    <textarea name="outputArea" id="outputArea"></textarea>
    <p class="finishMessage" id="finishMessage" name="finishMessage"></p>
</form>
于 2012-05-17T17:59:26.887 に答える