问题描述:

I have a page which contains a 10 items(formatted list).Here in this page I need to add check box for each item and add the item as the value to each check box.when the user click on the check box the selected value should be passed to a new page.Can anyone help me how to add a check box for the innerHTML in java script.

Code:

 var newsletter=document.getElementById("block-system-main");

var districolumn=getElementsByClassName('view-id-_create_a_news_letter_',newsletter,'div');

if(districolumn!=null)

{

var newsletterall=newsletter.getElementsByTagName('li');

alert(newsletterall[0].innerHTML);

var all=newsletter.innerHTML;

newsletter.innerHTML="<input type='button' onclick='changeText()' value='Change Text'/>";

}

function changeText()

{

alert("dfgsdg");

}

网友答案:

I don't exactly understand what each part of your code is doing, but i'll try and give a general answer:

In your HTML, do something like this:

<form id="myForm" action="nextPage.com">
    <div id="Boxes"></div>
</form>

Change the above names to wherever you want your checkboxes to be written.

And your function:

function changeText()
{
    for(var i=0 ; i < newsletterall.length ; i++)
    {
        var inner = document.getElementById("Boxes").innerHTML;
        var newBox = ('<input type="checkbox" name="item[]" value="' + newsletter[i] + '>' + newsletterall[i]);
        document.getElementById("Boxes").innerHTML = inner + newBox;
    }
document.getElementById("myForm").submit();
}

The last line of code submits the checkboxes automatically. If you don't want that, remove that line, and add a submit button to the form myForm.

网友答案:

$('ul#list li').each(
    function() {
        var me = $(this),
            val = me.html(),
            ckb = $('<input type="checkbox" />');

        ckb.click(function() {
            var where=val;
            window.location.href='http://google.com/?'+where; 
        });

        me.html('');
        me.append(ckb).append($('<span>'+val+'</span>'));
    }
);
相关阅读:
Top