开发者

jQuery put content of textbox inside a div

This is the html markup

<head>
    <script type="text/javascript" src="jquery-1.4.2.min.js">
    </script>
    <style type="text/css">
        div{ padding:4px; }
    </style>
</head>

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

        function() {
            var counter = 2;
            $('a.add').live('click', function() {
                if (counter > 5) {
                    alert("Only 5 textboxes allowed");
                    return false;
                }
                var newRow =
                $(this).closest('tr').clone().appendTo('#gridInAnswers').find('input').val('');

                var i = 0;
                $('#gridInAnswers input').each(function() {
                    $(this).attr("id", "Col"+ (i++));

                });

                $(this).text('Remove').removeClass('add').addClass('remove');
                counter++;
            });
            $('a.remove').live('click', function() {
                $(this).closest('tr').remove();
                counter--;
                var i=0;
                 $('#gridInAnswers input').each(fun开发者_StackOverflow中文版ction() {
                    $(this).attr("id", "Col"+ (i++));

                });
            });
               $("#getButtonValue").click(function() {
               var test='';
               for(var i=0;i<5;i++){
                   var minValue = $('#Col'+(i++)).val().trim();
                   var maxValue = $('#Col'+i).val().trim();

                   if((minValue=='' && maxValue !='')||(minValue!='' && maxValue !='' && minValue > maxValue)){
                    alert('Alerting...');
                    }
                }




                var gridInAnswerValuesHtml = "";
                $(".grdAns").each(function() {
                    //ansString += this.value+",";
                    gridInAnswerValuesHtml += "<input type = \"hidden\" name = \"gridInAnswers\" value = \"";
                    gridInAnswerValuesHtml += this.value;
                    gridInAnswerValuesHtml += "\">";
                });
                alert(gridInAnswerValuesHtml);

            });


        });
    </script>
    </head>

    <body>
    <div id="blankDiv"></div>
        <table id="gridInAnswers">
            <tr>
                <th>
                    Min Value
                </th>
                <th>
                    Max Value
                </th>
            </tr>
            <tr>
                <td>
                    <input type="text" name="col1" id="Col0" class="grdAns" />
                </td>
                <td>
                    <input type="text" name="col1" id="Col1" class="grdAns" />
                </td>
                <td>
                    <a href="#" class="add">Add</a>
                </td>
            </tr>
            <tr>
                <input type='button' value='Get TextBox Value' id='getButtonValue'>
            </tr>
        </table>
    </body>

on click of the add button I want to remove the textbox but its contents should get displayed say inside a div. i.e. I want to stop user from modifying this but disabling the textbox is not an option. How do I achieve this ?


Basically what you need to do is to do the replacement as soon as you click on the add link. The replacement is really simple.

// Do the magic here (Kuroir)
$(this).parent().parent().find('input').each(function(){
    $(this).replaceWith('<div class="inactive">' + $(this).val() + '</div>');
});

Basically, you're going back to the tr level, then finding the input and doing the replacement for that row.

Working Solution: http://jsfiddle.net/kuroir/SXppg/


To get content from an <input> and place it in a <div>, you want something like:

$('#target-div-id').text(    
    $('#Col0').val()
);

I'm reasonably sure that's what you're trying to do.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜