如何在javascript中的dot html()中将div属性作为参数传递

时间:2014-05-12 03:23:43

标签: javascript jquery html

我遇到的问题是我传递的textarea id无效。我尝试使用alert()检查它正在采取的id value,但警报是空的。我该怎么办呢?

JavaScript的:

$(document).ready(function(){              
    $('#select').change(function () {
        if ($('#select option:selected').text() == "Form1"){
            $("#text1").html("<textarea></textarea>");
            console.log("display text1 only");
            $("#text2").hide();
            $("#text1").show();   
        }
        else if ($('#select option:selected').text() == "Form2"){
            $("#text2").html("<textarea id='character1' rows='8' cols='30' maxlength='500' placeholder='Summary2' ></textarea><div id='textarea_feedback1'></div>"); 
            $("#text1").hide();
            $("#text2").show();
        }
        else if ($('#select option:selected').text() == "Select an Option"){
            $('#text1').hide();
            $('#text2').hide();
        }
    });   
});

2 个答案:

答案 0 :(得分:0)

我认为你的选择框的HTML是这样的,

<select id="select">
<option value='Select an Option'>Select an Option</option>
<option value='Form1'>Form1</option>
<option value='Form2'>Form2</option>
</select>

所以脚本应该像这样改变

 <script type ="text/javascript">
    $(document).ready(function(){

        $('#select').change(function () {
            if ($('select#select').val() == "Form1"){
                $("#text1").html("<textarea></textarea>");
                console.log("display text1 only");
                $("#text2").hide();
                $("#text1").show();      
            }
            else if ($('select#select').val() == "Form2"){
                $("#text2").html("<textarea id='character1' rows='8' cols='30' maxlength='500' placeholder='Summary2' ></textarea><div id='textarea_feedback1'></div>"); 
                $("#text1").hide();
                $("#text2").show();
            }
            else if ($('select#select').val() == "Select an Option"){
                $('#text1').hide();
                $('#text2').hide();
            }
        }); 

    });
    </script>

答案 1 :(得分:0)

<强> DEMO

HTML:

<select id="select"> 
   <option value='Select an Option'>Select an Option</option>
    <option value='Form1'>Form1</option>
    <option value='Form2'>Form2</option>
</select>
    <div id="text1">

    </div>
    <div id="text2">

    </div> 

Jquery:

 $(document).ready(function(){

        $('#select').change(function () {
          var value = $("select#select option:selected").val(); 
          if(value==="Form1") {
         var $text1 = $("#text1").html("<textarea></textarea>");
                $("#text2").hide();
                $("#text1").show();    
          }
       else if (value === "Form2"){
                $("#text2").html("<textarea id='character1' rows='8' cols='30' maxlength='500' placeholder='Summary2' ></textarea><div id='textarea_feedback1'></div>"); 
                $("#text1").hide();
                $("#text2").show();
            }
            else if (value == "Select an Option"){
                $('#text1').hide();
                $('#text2').hide();
            }
        }); 

    });