Jquery 显示文本框而不单击按钮

Jquery 显示文本框而不单击按钮,jquery,ruby-on-rails,Jquery,Ruby On Rails,在我的设置页面中,我有一些动态控件,如下所示: <% @user_education.each do |user_edu| %> <script type="text/javascript"> $(document).ready(function() { $("#AddSch").click(function() { var field = $("#field").val();

在我的设置页面中,我有一些动态控件,如下所示:

<% @user_education.each do |user_edu| %>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#AddSch").click(function() {
                var field = $("#field").val();
                var tb_fromEducation = "<input type='text' style='width:50px' name='ParametersFromSch' value='<%= user_edu.SchoolFrom %>' />";

                var tb_ToEducation = "<input type='text' style='width:50px' name='ParametersToSch' value='<%= user_edu.SchoolTo %>' />";


                var newRow1 = "<tr><td align='center' style='font-size: large; color: #212121;' >"
                + tb_fromEducation + " to " + tb_ToEducation + "</td></tr>"
                + "<tr><td align='center' style='font-size:large;color:#212121;' >"
                newRow1 += "<input type='button' class='btn_rmvsch' value='Remove'/></td></tr>";


                var input1 = "<input name='parametersSch' id='field' type='text' value='<%= user_edu.SchoolName %>' />"
                var newRow = "<tr><td align='center' style='font-size: x-large; color: #212121;' >" + input1 + "</td></tr>";
                $('#ControlsSch').append(newRow);
                $('#ControlsSch').append(newRow1);
            });

            $('#ControlsSch').on('click', '.btn_rmvsch', function() {
                var index = $(this).closest('tr').index() + 2
                $('#ControlsSch tr:nth-child(n+' + (index - 3) + ')').remove();
                return false;
            });


        });
    </script>
<% end %>

$(文档).ready(函数(){
$(“#AddSch”)。单击(函数(){
变量字段=$(“#字段”).val();
var tb_fromEducation=“”;
var tb_ToEducation=“”;
var newRow1=“”
+tb_从教育+”到“+tb_到教育+”
+ ""
newRow1+=“”;
var input1=“”
var newRow=“”+输入1+”;
$('#ControlsSch').append(newRow);
$('#ControlsSch').append(newRow1);
});
$('#ControlsSch')。on('click','.btn#u rmvsch',function(){
var index=$(this).closest('tr').index()+2
$('#ControlsSch tr:n个子项(n++(索引-3)+')).remove();
返回false;
});
});
上面的代码创建了3个文本框,我从数据库中获取值并将其放入文本框中,它在文本框中显示值,下面是new.html.erb的代码

<td align="center">
                            <table id="SchoolControls" cellpadding="10" cellspacing="10">
                            </table>
                            <table id="ControlsSch" cellpadding="10" cellspacing="10">
                            </table>
                            <input id="AddSch" type="button" value="Add" />
                        </td>


在上面的代码中,我使用“ControlsCh”表来显示动态控件,问题是在单击“添加”按钮后,文本框是可见的,我希望当我转到“设置”页面时,文本框已经可见,而没有单击“添加”按钮。请帮助我,等待答复。谢谢。

试试这个:

<% @user_education.each do |user_edu| %>
    <script type="text/javascript">
        $(document).ready(function() {
                var field = $("#field").val();
                var tb_fromEducation = "<input type='text' style='width:50px' name='ParametersFromSch' value='<%= user_edu.SchoolFrom %>' />";

                var tb_ToEducation = "<input type='text' style='width:50px' name='ParametersToSch' value='<%= user_edu.SchoolTo %>' />";


                var newRow1 = "<tr><td align='center' style='font-size: large; color: #212121;' >"
                + tb_fromEducation + " to " + tb_ToEducation + "</td></tr>"
                + "<tr><td align='center' style='font-size:large;color:#212121;' >"
                newRow1 += "<input type='button' class='btn_rmvsch' value='Remove'/></td></tr>";


                var input1 = "<input name='parametersSch' id='field' type='text' value='<%= user_edu.SchoolName %>' />"
                var newRow = "<tr><td align='center' style='font-size: x-large; color: #212121;' >" + input1 + "</td></tr>";
                $('#ControlsSch').append(newRow);
                $('#ControlsSch').append(newRow1);


            $('#ControlsSch').on('click', '.btn_rmvsch', function() {
                var index = $(this).closest('tr').index() + 2
                $('#ControlsSch tr:nth-child(n+' + (index - 3) + ')').remove();
                return false;
            });


        });
    </script>
<% end %>

$(文档).ready(函数(){
变量字段=$(“#字段”).val();
var tb_fromEducation=“”;
var tb_ToEducation=“”;
var newRow1=“”
+tb_从教育+”到“+tb_到教育+”
+ ""
newRow1+=“”;
var input1=“”
var newRow=“”+输入1+”;
$('#ControlsSch').append(newRow);
$('#ControlsSch').append(newRow1);
$('#ControlsSch')。on('click','.btn#u rmvsch',function(){
var index=$(this).closest('tr').index()+2
$('#ControlsSch tr:n个子项(n++(索引-3)+')).remove();
返回false;
});
});

问题不清楚。。。为什么不在$(document.ready();)上添加内容?很简单,当我在“设置”页面时,动态控件是可见的,无需点击“添加”,但这就是我想告诉你的。。。只需删除click habdler并将代码直接放在$(document).ready()下即可@桑妮:你可能误解了OP的问题。他想要的是在点击AddSch`时创建动态控件,但在当前代码中,这些控件出现时,不会在该按钮上启动任何点击事件。
AddSch
。@Hassanikhan:我认为点击事件是在代码的某个地方触发的,您可能需要检查完整的代码。@Hassanikhan ok,那么请你把它标为答案好吗。