2011-07-29 8 views
1

以下のフォームがあります。jQueryセレクタを選択する際に助けが必要

<form action="" method="post" id="save-user"> 
    <button type="button" name="save-user">Save</button> 
    <button type="button" name="save-user-close">Save & Close</button> 
    <input type="text" name="name" placeholder="Enter your name" required/> 
    <input type="email" name="email" placeholder="Enter your email" required/> 
</form> 

は、私はid属性を使用していないとして、私は、この場合には、それを使用したくない、すべての入力とボタン要素のためのjQueryのセレクタを作成するにはどうすればよいの混乱しています。どのように私は4つのフォーム要素のすべてのセレクタを取得または作成するのですか?

ありがとうございました。

答えて

2

上記の回答はすべてOKですが、高度なセレクタ(as pseudo classes, attribute selectors and more)を使用して学習することができます。あなたのニーズに合った - - 要素にIDやクラスをバインドする必要withouthあなたは唯一のjQueryのセレクタを使用して、より複雑なクエリを作成することができます

// all the buttons that are children of the container 
//with the attribute id equal to "save-user" 
$("#save-user").find('button'); 

// all the buttons that have their name attribute starting with "save-user" 
$('button[name^=save-user]'); 

// same as before, only that we filter the results 
// by checking if they contain the text "Save" 
$('#save-user').find('button').filter(':contains(Save)'); 


はここにあなたのためのいくつかの例の場合あり。もちろん、主な欠点は効率ですが、特別なセレクタを乱用しないと、jqueryのコーディングスタイルはかなりクールになる可能性があります。

1

どのようにあなたが入力のためのボタンと$(':input[name="name"]')ため$(':button[name="save-user"]')を使用することができます $('#save-user button,#save-user input').blah();

1
$("#save-user").find("button, input") 
関連する問題