检查复选框值时,Jquery如何获取文本框数组值?

时间:2022-11-28 20:43:12

I want to get only textbox value where checked by checkbox, all textbox and checkbox from database array.

我想只通过复选框,所有文本框和数据库数组中的复选框来获取文本框值。

<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>

New Question, but almost same with old question, the questions is, in my array PHP code, I try to divide into 3 cells, and when I try to get textbox value, all value textbox in the same row is the same value. so this is the problem code:

新问题,但几乎与旧问题相同,问题是,在我的数组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 } ?>

And the Jquery is the same with last jquery suggestion from you Guru. and now any suggestion for jquery process? thanks.

Jquery和你Guru的最后一个jquery建议是一样的。现在对jquery进程的任何建议?谢谢。

The html code maybe like this mate:

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>

1 个解决方案

#1


2  

You don't need to iterate through each textbox since you will have only one textbox and checkbox in each tr. When you check checked property of each checkbox and if it is checked get the textbox value associated with it as below:

您不需要遍历每个文本框,因为每个tr中只有一个文本框和复选框。当您检查每个复选框的checked属性时,如果选中它,请获取与其关联的文本框值,如下所示:

DEMO

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);
}

UPDATE

Get the value of textbox like this:

像这样获取文本框的值:

bookpriceArr.push($(this).parents("td").next().find(".bookprice").val())

It will traverse back to the parent td of checkbox and then finds next element of parent td witch is a td which contains textbox in this case and then finds textbox with class .bookprice which exists inside it. Now you have to be careful that there should not be any other element in between parent td of checkbox and parent td of its corresponding textbox

它将遍历复选框的父td,然后查找父td巫婆的下一个元素是一个td,在这种情况下包含文本框,然后找到包含在其中的类.bookprice的文本框。现在你必须要小心,在复选框的父td和相应文本框的父td之间不应该有任何其他元素

#1


2  

You don't need to iterate through each textbox since you will have only one textbox and checkbox in each tr. When you check checked property of each checkbox and if it is checked get the textbox value associated with it as below:

您不需要遍历每个文本框,因为每个tr中只有一个文本框和复选框。当您检查每个复选框的checked属性时,如果选中它,请获取与其关联的文本框值,如下所示:

DEMO

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);
}

UPDATE

Get the value of textbox like this:

像这样获取文本框的值:

bookpriceArr.push($(this).parents("td").next().find(".bookprice").val())

It will traverse back to the parent td of checkbox and then finds next element of parent td witch is a td which contains textbox in this case and then finds textbox with class .bookprice which exists inside it. Now you have to be careful that there should not be any other element in between parent td of checkbox and parent td of its corresponding textbox

它将遍历复选框的父td,然后查找父td巫婆的下一个元素是一个td,在这种情况下包含文本框,然后找到包含在其中的类.bookprice的文本框。现在你必须要小心,在复选框的父td和相应文本框的父td之间不应该有任何其他元素