Jquery 如何在选中复选框值时获取文本框数组值?
How Jquery get textbox array value when checkbox value checked?
我只想获取复选框选中的文本框值,所有文本框和数据库数组中的复选框。
<tr><?php foreach($arrbook as $book) { ?>
<td><input type="checkbox" name="book" class="book" value="<?php echo $book['id_book']; ?>"/> <?php echo $book['name_book'];?></td>
<td><input type="text" name="bookprice"class="bookprice" value="<?php echo $book['book_price']; ?>"/</td>
<?php } ?></tr>
<td><div class="button"><a href="#" onClick="submitbookdata();">Save !</a></div></td></tr>
JS
<script>
function submitbookdata() {
var bookidArr = [];
var bookpriceArr = [];
$('.book').each(function() {
if (this.checked) {
var current = $(this).val();
$(this).parents("tr").find(".bookprice").each(function)(index, n) {
if ($.each('n').val() == current) {
bookpriceArr.push(this.value);
}
});
}
bookidArr.push($(this).val());
});
alert(bookpriceArr);
}
</script>
新问题,但与旧问题几乎相同,问题是,在我的数组 PHP 代码中,我尝试划分为 3 个单元格,当我尝试获取文本框值时,所有值文本框在同一行是相同的值。所以这是问题代码:
<?php $cell_index = 1; foreach($arrbook as $book)
{
if ($cell_index == 1)
{
<tr>
}
<td>
<td><input type="checkbox" name="book" class="book" value="<?php echo $book['id_book']; ?>"/> <?php echo $book['name_book'];?></td>
<td><input type="text" name="bookprice"class="bookprice" value="<?php echo $book['book_price']; ?>"/</td>
<?php echo "\n";?>
<?php if ( ++$cell_index > 3 )
{ ?>
</tr>
<?php echo "\n"; $cell_index = 1; } ?>
<?php } ?>
Jquery 与上次 jquery 大师的建议相同。现在对 jquery 流程有什么建议吗?谢谢。
html 代码可能像这样的朋友:
<table>
<tr>
<th>COLUMN 1</th>
<th>COLUMN 2</th>
<th>COLUMN 3</th>
<th>COLUMN 4</th>
</tr>
<tr>
<td><input type="checkbox" name="book" class="book" value="Checkbox 1"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 2"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 3"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
</tr>
<tr>
<td><input type="checkbox" name="book" class="book" value="Checkbox 4"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="300"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 5"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 6"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
</tr>
<tr>
<td><input type="checkbox" name="book" class="book" value="Checkbox 7"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="400"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 8"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="400"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 9"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="400"/></td>
</tr>
<tr>
<td><div class="button"><a href="#" onClick="submitbookdata();">Save !</a></div></td>
</tr>
</table>
您不需要遍历每个 textbox
,因为每个 tr
中只有一个 textbox
和 checkbox
。当您检查每个 checkbox
的 checked
属性 时,如果检查了它,则获取与其关联的 textbox
值,如下所示:
function submitbookdata() {
var bookidArr = [];
var bookpriceArr = [];
$('.book').each(function() {
if ($(this).is(':checked')) {// check the checked property with .is
var current = $(this).val();
bookpriceArr.push($(this).parents("tr").find(".bookprice").val()) //get the input textbox associated with it
}
bookidArr.push($(this).val());
});
alert(bookpriceArr);
}
更新
像这样获取 textbox
的值:
bookpriceArr.push($(this).parents("td").next().find(".bookprice").val())
会遍历回checkbox
的父td
,然后找到父td
的下一个element
是一个td
在这种情况下包含 textbox
,然后找到 textbox
和 class .bookprice
存在于其中。现在你必须小心,在 checkbox
的父 td
和对应的 textbox
[=34 的父 td
之间不应该有任何其他 element
=]
我只想获取复选框选中的文本框值,所有文本框和数据库数组中的复选框。
<tr><?php foreach($arrbook as $book) { ?>
<td><input type="checkbox" name="book" class="book" value="<?php echo $book['id_book']; ?>"/> <?php echo $book['name_book'];?></td>
<td><input type="text" name="bookprice"class="bookprice" value="<?php echo $book['book_price']; ?>"/</td>
<?php } ?></tr>
<td><div class="button"><a href="#" onClick="submitbookdata();">Save !</a></div></td></tr>
JS
<script>
function submitbookdata() {
var bookidArr = [];
var bookpriceArr = [];
$('.book').each(function() {
if (this.checked) {
var current = $(this).val();
$(this).parents("tr").find(".bookprice").each(function)(index, n) {
if ($.each('n').val() == current) {
bookpriceArr.push(this.value);
}
});
}
bookidArr.push($(this).val());
});
alert(bookpriceArr);
}
</script>
新问题,但与旧问题几乎相同,问题是,在我的数组 PHP 代码中,我尝试划分为 3 个单元格,当我尝试获取文本框值时,所有值文本框在同一行是相同的值。所以这是问题代码:
<?php $cell_index = 1; foreach($arrbook as $book)
{
if ($cell_index == 1)
{
<tr>
}
<td>
<td><input type="checkbox" name="book" class="book" value="<?php echo $book['id_book']; ?>"/> <?php echo $book['name_book'];?></td>
<td><input type="text" name="bookprice"class="bookprice" value="<?php echo $book['book_price']; ?>"/</td>
<?php echo "\n";?>
<?php if ( ++$cell_index > 3 )
{ ?>
</tr>
<?php echo "\n"; $cell_index = 1; } ?>
<?php } ?>
Jquery 与上次 jquery 大师的建议相同。现在对 jquery 流程有什么建议吗?谢谢。
html 代码可能像这样的朋友:
<table>
<tr>
<th>COLUMN 1</th>
<th>COLUMN 2</th>
<th>COLUMN 3</th>
<th>COLUMN 4</th>
</tr>
<tr>
<td><input type="checkbox" name="book" class="book" value="Checkbox 1"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 2"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 3"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
</tr>
<tr>
<td><input type="checkbox" name="book" class="book" value="Checkbox 4"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="300"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 5"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 6"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="200"/></td>
</tr>
<tr>
<td><input type="checkbox" name="book" class="book" value="Checkbox 7"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="400"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 8"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="400"/></td>
<td><input type="checkbox" name="book" class="book" value="Checkbox 9"/></td>
<td><input type="text" name="bookprice"class="bookprice" value="400"/></td>
</tr>
<tr>
<td><div class="button"><a href="#" onClick="submitbookdata();">Save !</a></div></td>
</tr>
</table>
您不需要遍历每个 textbox
,因为每个 tr
中只有一个 textbox
和 checkbox
。当您检查每个 checkbox
的 checked
属性 时,如果检查了它,则获取与其关联的 textbox
值,如下所示:
function submitbookdata() {
var bookidArr = [];
var bookpriceArr = [];
$('.book').each(function() {
if ($(this).is(':checked')) {// check the checked property with .is
var current = $(this).val();
bookpriceArr.push($(this).parents("tr").find(".bookprice").val()) //get the input textbox associated with it
}
bookidArr.push($(this).val());
});
alert(bookpriceArr);
}
更新
像这样获取 textbox
的值:
bookpriceArr.push($(this).parents("td").next().find(".bookprice").val())
会遍历回checkbox
的父td
,然后找到父td
的下一个element
是一个td
在这种情况下包含 textbox
,然后找到 textbox
和 class .bookprice
存在于其中。现在你必须小心,在 checkbox
的父 td
和对应的 textbox
[=34 的父 td
之间不应该有任何其他 element
=]