开发者

Getting multiple elements in JavaScript

There is a drop down menu in the web application when the users select any one of the option from the drop down menu, I have to generate a text box and check box. I have implemented onSelect on drop down menu and I have called a function which actually calls the function which adds the text but I need to generate the check box along with the textbox.

I tried to call one more function but that's not working I also tried to using 开发者_StackOverflow中文版&& even that is not working and I have tried to call a function inside the function even that is not working. How do I do this?

This is the code:

<SCRIPT language="javascript">
function add(type) {
 
    //Create an input type dynamically.
    var element = document.createElement("input");
    
    
    //Assign different attributes to the element.
    element.setAttribute("type", type);
    element.setAttribute("value", type);
    element.setAttribute("name", type);
    
    var foo = document.getElementById("fooBar");
 
    //Append the element in page (in span).
    foo.appendChild(element);
    //var element=document.createElement("checkbox");
    //var foo=document.getElementById("foobar");
    //foo.appendChild(element);
    var a=1;

    if(a==1){
        function add2(type) {
 
            //Create an input type dynamically.
        var chkbox = document.createElement("checkbox");
            //Assign different attributes to the element.
            
        chkbox.setAttribute("type", type);
            chkbox.setAttribute("value", type);
            chkbox.setAttribute("name", type);
    
            //var foo = document.getElementById("fooBar");
        //Append the element in page (in span).
            appendChild(chkbox);
        }
    }
}
</SCRIPT>


the best way to do it will be call a function with arguments on select from drop down menu but you have to generate arguments for each link in drop down menu

function fuc1( arg1 , arg2, ... )
{
            //you can use these agruments to genetate attrs of input and other things
    var input = $("<input></input>").attr({'type': 'text', 'id': someid, 'class': 'someclass' , ... }).val(someval);
    var chbox = $("<input></input>").attr({'type': 'checkbox', 'id': someid, 'class': 'someclass' , ... });

    $(input).appendTo(something);
    $(chbox).appendTo(something);
}

it generally works this way. if you submit your code then it might a bit help you

updates::

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
function fuc1()
{
        //you can use these agruments to genetate attrs of input and other things
    var container = $("<div></div>").appendTo($('#content'));
    var input = $("<input></input>").attr({'type': 'text'});
    var chbox = $("<input></input>").attr({'type': 'checkbox'});

    $(input).appendTo(container);
    $(chbox).appendTo(container);
}

</script>

<div id="content"></div>
<a href="javascript: fuc1()">add</a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜