简体   繁体   English

如何使用jquery获取php中文本字段和隐藏字段的循环值

[英]how can i get the value of the loop both text fields and hidden fields in php using jquery

How can I get the values of each input field (both hidden and visible) using jQuery? 如何使用jQuery获取每个输入字段(隐藏和可见)的值?

<?php 
     $date   = date('m-d-Y');
     $ts     = strtotime($date);
     $year   = date('o', $ts);
     $week   = date('W', $ts);

     for($i = 1; $i <= 7; $i++) {
     ?><td><?php
     $ts = strtotime($year.'W'.$week.$i);
     ?><input type="text" name="days" id="days" />
      <input type="hidden" name="date_now" id="date_now" value="<?php print date("Y-m-d", $ts); ?>" />
    </td><?php
    } 
?> 

Are all the inputs produced by the loop intended to be submitted as part of one form? 环路产生的所有输入是否都要作为一个表单的一部分提交? Why not collate them into an array ? 为什么不将它们整理成阵列

Change the names from days to days[] and from date_now to date_now[] , respectively. 分别将名称从days更改为days[]和从date_nowdate_now[]

When embedding php control structures directly into html, consider using the alternative syntax for more clarity. 将php控件结构直接嵌入到html中时,请考虑使用替代语法以获得更清晰。

If you embed the current loop counter to the id of each input, you'll be able select each one individually. 如果将当前循环计数器嵌入到每个输入的id中,您将能够单独选择每个输入。

If you add a class attribute to each input, you can select them as a group. 如果为每个输入添加class属性,则可以将它们选择为组。

You can eliminate the extra assignment to $ts in each loop iteration by passing the result of strtotime() directly to the second parameter of date() . 通过将strtotime()的结果直接传递给date()的第二个参数,可以消除每次循环迭代中$ts的额外赋值。

Since it's not totally clear what your ultimate intent was, here is your initial code sample modified with all of my suggestions applied: 由于您的最终意图并不完全清楚,因此以下是您修改的初始代码示例,其中包含我的所有建议:

<?php 
$date = date('m-d-Y');
$ts = strtotime($date);
$year = date('o', $ts);
$week = date('W', $ts);

for($i = 1; $i <= 7; $i++): 
$dt = date("Y-m-d", strtotime($year.'W'.$week.$i));
?>
<td>
<input type="text" name="days[]" id="days<?=$i?>" class="days" />
<input type="hidden" name="date_now[]" id="date_now<?=$i>" value="<?=$dt?>" class="date_now" />
</td>
<?php endfor; ?>

From the above code, you can either select one of the inputs individually like this: 从上面的代码中,您可以单独选择其中一个输入,如下所示:

$("#days3").val();

Or, you can select all of the hidden inputs like this: 或者,您可以选择所有隐藏的输入,如下所示:

var dates_now = [];
$(".date_now").each(function(i, input) {
    dates_now.push(input.val());
});

changing the id='days' and id='date_now' to classes (class='days' and class='date_now') assuming you want all the values, we can put them into an array like this: 将id ='days'和id ='date_now'更改为类(class ='days'和class ='date_now'),假设您需要所有值,我们可以将它们放入如下数组中:

var days_array     = [];
var date_now_array = [];

$('.days').each(function() {
    days_array.push($(this).val());
});

$('.date_now').each(function() {
    date_now_array.push($(this).val());
});

now the variables days_array and date_now_array contain all the values. 现在变量days_arraydate_now_array包含所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM