2016-12-11 3 views
0

私はJavascriptが私もこれをしなかったが、今の行Jqueryのアラートボックスで、チェックされた行のテキストボックスからデータを取得する方法は?

$("#add").click(function() { 
    $("#myTable").last().append("<tr><td width='5%'><input type='checkbox'>          
    </td><td><input type='text' id='1stid'> 
    </td><td><input type='text' id='2ndid'></td><td><input type='text'id='3rdid'></td></tr>"); 
}); 

を追加するための

私はボタンを押すたびメートルの警告ボックスにチェックされたすべての行を表示したいが、私はここで何か間違ったことをやっている

警告ボックスは

$("#view").click(function() { 
    $('input[type="checkbox"]:checked'); 
    var n=$(this).closest('tr').text(); 
    alert(n); 
}); 

マイHTML

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 

 
<!-- Optional theme --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 
 
    <link rel="stylesheet" href="Project.css"> 
 
</head> 
 
<body> 
 
    <div class="container"> 
 
     <button id="add">Add</button> <button id="deleteLast">Remove </button> <button id="delete"> Remove All</button> <button id="view">View</button> 
 

 
     <table class="table table-bordered" id="myTable"> 
 
      <tr> 
 
       <th width="5%"></th> 
 
       <th>First name</th> 
 
       <th>Last name</th> 
 
       <th>Profession</th> 
 
      </tr> 
 
     </table> 
 
    </div> 
 
    
 
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 
 
<script type="text/javascript" src="Project.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
 
</body> 
 
</html>
空であります10

+1

を反復するためにjQueryのeach方法、$("input[type='checkbox']:checked").each(...)を使用し、要素の配列/リストを返しますので、あなたのHTMLコードを投稿してくださいので、私たちもう少し文脈がある。 – gyre

+0

@gyre htmlコードは4つのボタンと1つの単純な行しか持っていません。私のボタンを使って動的に行を追加していて、今削除しています。このボタンにはまっているのですが、id(#view) –

+1

でもjsfiddle誰かがあなたの問題を潜在的に解決できる人を気にしない限り、あなたの質問をスキップすることができます。 –

答えて

0

$("input[type='checkbox']:checked")は結果を通じて

$("#view").click(function() { 
 
    $("input[type='checkbox']:checked").each(function(index) {  
 
     $('td input[type=text]', $(this).closest('tr')).each(function(index2) { 
 
      
 
      alert($(this).val()); 
 
      
 
     }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table> 
 
    <tr class="test"> 
 
    <td width='5%'> 
 
     <input type='checkbox'> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='11stid' value="1 first"> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='12ndid' value="1 second"> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='13rdid' value="1 third"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <td width='5%'> 
 
     <input type='checkbox'> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='21stid' value="2 first"> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='22ndid' value="2 second"> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='23rdid' value="2 third"> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <td width='5%'> 
 
     <input type='checkbox'> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='31stid' value="3 first"> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='32ndid' value="3 second"> 
 
    </td> 
 
    <td> 
 
     <input type='text' id='33rdid' value="3 third"> 
 
    </td> 
 
    </tr> 
 
</table> 
 

 
<button id="view">Checked</button>

+0

その仕事の仲間 –

+0

@AhsanAli上記の仕事として、それはあなたのコードで動作しないという意味ですか? – LGSon

+0

はい、すべてのアラートボックスに "表示"するだけです@LGSon –

関連する問題