jquery自动完成动态生成的文本框

时间:2012-02-27 11:19:37

标签: jquery asp.net-mvc autocomplete telerik-mvc

我是jquery的新手,我正在开发一个需要使用自动完成工具动态生成文本框的网页。 我不知道绑定事件如何与生成的文本框。 我的代码如下:

 $(document).ready(function () {

    var counter = 1;

    $(".addButton").live("click", function () {

        var newTextBoxDiv = $(document.createElement('div'))
  .attr("id", 'TextBoxDiv' + counter);

        newTextBoxDiv.html('<TABLE><TR><TD>' + '<input type="text" name="textbox' + counter + '" id="textbox' + counter + '" value="" ></TD>&nbsp;<TD><a href="#" value="addButton" class="addButton">Add</a>&nbsp;</TD></TR></TABLE>');

        newTextBoxDiv.appendTo("#TextBoxesGroup");
        counter++;
    });
});

并发布查询

function fillTextBox(text) {
    $.ajax(
        {
            type: 'POST',
            url: '@Url.Action("_AutoCompleteAjaxLoading", "CommandEntity")',
            async: true,
            data: { text: text},
            dataType: "json",
            traditional: true,
            success: function(data) {
                //what I should do here?
            },
            error: function(xhr, ajaxOptions, thrownError) {

            }
        });
}

2 个答案:

答案 0 :(得分:3)

您可以使用jquery ui autocomplete插件:

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult _AutoCompleteAjaxLoading(string term)
    {
        var data = new[] { "item 1", "item 2", "item 3" };
        return Json(data, JsonRequestBehavior.AllowGet);
    }
}

查看:

<script type="text/javascript">
    $(function () {
        setupAutoComplete();
        var counter = 2;
        $('#TextBoxesGroup').delegate('a.addButton', 'click', function (evt) {
            evt.preventDefault();
            $('<table/>', {
                html: $('<tr/>', {
                    html: $('<td/>', {
                        html: $('<input/>', {
                            type: 'text',
                            id: 'textbox' + counter,
                            name: 'textbox' + counter
                        })
                    }).after($('<td/>', {
                        html: $('<a/>', {
                            href: '#',
                            'class': 'addButton',
                            text: 'Add'
                        })
                    }))
                })
            }).appendTo('#TextBoxesGroup');
            setupAutoComplete();
            counter++;
        });
    });

    var setupAutoComplete = function () {
        $(':text[name^="textbox"]').autocomplete({
            source: '@Url.Action("_AutoCompleteAjaxLoading")',
            minLength: 2,
            select: function (event, ui) {

            }
        });
    };
</script>

<div id="TextBoxesGroup">
    <table>
        <tr>
            <td>
                <input type="text" name="textbox1" id="textbox1" value="" />
            </td>
            <td>
                <a href="#" class="addButton">Add</a>
            </td>
        </tr>
    </table>
</div>

答案 1 :(得分:1)

我不确定你还需要什么。尝试添加像

这样的行
$('#textbox' + counter).keypress( function(){
  // here trigger autocomplete event
} )

或者

 $('#textbox' + counter).keypress( fillTextBox( $(this).val() ) ) 
相关问题