Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery MVC 3-动态添加文本框_Jquery_Asp.net Mvc 3 - Fatal编程技术网

Jquery MVC 3-动态添加文本框

Jquery MVC 3-动态添加文本框,jquery,asp.net-mvc-3,Jquery,Asp.net Mvc 3,我有一个场景,用户可以选择他们想要输入信息的测试。 例如: 当选择Test1,Test3时,我需要用这两个文本框字段Test1,Test2创建一个表单。我在考虑通过用户选择的选项进行循环,然后通过jquery进行显示、隐藏,但不确定这是否是最好的方法。是否有一种优雅的方式来显示用户选择的字段 像这样的事情可能很简单: $(':checkbox').each(function(){ //loop through your checkboxes if($(this).prop('check

我有一个场景,用户可以选择他们想要输入信息的测试。 例如:


当选择Test1,Test3时,我需要用这两个文本框字段Test1,Test2创建一个表单。我在考虑通过用户选择的选项进行循环,然后通过jquery进行显示、隐藏,但不确定这是否是最好的方法。是否有一种优雅的方式来显示用户选择的字段

像这样的事情可能很简单:

$(':checkbox').each(function(){ //loop through your checkboxes

    if($(this).prop('checked')) { //if the current checkbox is checked

        var text = $(this).text(); //Test1, Test2 or whatever it might be. Would probably be better to store as a data-attribute on the checkbox

        var $textfield = $('<input/>').attr({ type: 'text' }).val(text); //generate a textfield with the selected text from the checkbox

        $('#yourForm').append($textfield); //add textfield to the form
    }

    $('#yourForm').show();

});
<ul id="textboxes">
    <li>
        <input type="checkbox" id="c1" name="c1" />                
        <input type="text" id="t1" name="t1" />
    </li>
    <li>
        <input type="checkbox" id="c2" name="c2" /> 
        <input type="text" id="t2" name="t2" style="display:none" />
    </li>
<ul>
显然,你可以在那里有更多的盒子。如果你想走一条更加动态的路线,有一条无限的路线,我会更倾向于真正添加字段


如何选择?复选框?是的复选框。目前我正在做一个。隐藏那些不需要的。很难没有看到更多的代码,但看看我的答案
<ul id="textboxes">
    <li>
        <input type="checkbox" id="c1" name="c1" />                
        <input type="text" id="t1" name="t1" />
    </li>
    <li>
        <input type="checkbox" id="c2" name="c2" /> 
        <input type="text" id="t2" name="t2" style="display:none" />
    </li>
<ul>
$('input[type="checkbox"]').click(function() {
    var id = $(this).attr('id').replace('c','');
    $('#t'+id).slideToggle(); 
});