How to assign HTML id to elements and how apply JavaScript to them?

时间:2021-02-25 13:37:58

I developed a project but now I wants to apply JavaScript to project. Take an example of Login Form. In that I have to assign HTML id to text-fields but HOW? and how I can access the text-fields in js file?

我开发了一个项目,但现在我想将JavaScript应用于项目。以登录表格为例。在那里我必须将HTML id分配给文本字段但是如何?以及如何访问js文件中的文本字段?

    <% @page_title = 'Staff Area Login' -%>

    <%= form_for(:stuff, :url => {:action => 'send_login'}) do -%>

    <p>Username:  <%= text_field_tag('username', params[:username]) %></p>

    <p>Password: <%= password_field_tag('password') %></p>

    <%= submit_tag("Log in") %>

    <% end -%>

3 个解决方案

#1


3  

You can use the :html hash.

您可以使用:html哈希。

<%= text_field_tag('username', params[:username], :html => {:id => "username_field"}) %>

#2


0  

Your form automatically takes id as new_staff if your form is running as new form and takes id as edit_staff if your form is running as edit form.

如果您的表单作为新表单运行,您的表单会自动将id作为new_staff,如果您的表单作为编辑表单运行,则将id作为edit_staff。

#3


0  

nt a ruby person iam but as far as i when you create a text-field like that. Ids are already asssigned have a look here
and if you wanna access a particular text-field by id then just use
document.getElementById('ID'); OR $('#ID') latter uses jquery

我是一个红宝石的人,但是当你创建像这样的文本字段时,我就是这样。 Ids已经被分配了,看看这里,如果你想通过id访问一个特定的文本字段,那么只需使用document.getElementById('ID'); OR $('#ID')后者使用jquery

#1


3  

You can use the :html hash.

您可以使用:html哈希。

<%= text_field_tag('username', params[:username], :html => {:id => "username_field"}) %>

#2


0  

Your form automatically takes id as new_staff if your form is running as new form and takes id as edit_staff if your form is running as edit form.

如果您的表单作为新表单运行,您的表单会自动将id作为new_staff,如果您的表单作为编辑表单运行,则将id作为edit_staff。

#3


0  

nt a ruby person iam but as far as i when you create a text-field like that. Ids are already asssigned have a look here
and if you wanna access a particular text-field by id then just use
document.getElementById('ID'); OR $('#ID') latter uses jquery

我是一个红宝石的人,但是当你创建像这样的文本字段时,我就是这样。 Ids已经被分配了,看看这里,如果你想通过id访问一个特定的文本字段,那么只需使用document.getElementById('ID'); OR $('#ID')后者使用jquery