0

我有一个这样的 HTML 选择标签:

<select name="since_date">
    <option value="<?php $sixmonths = date('Y-m-d', strtotime('-6 months')); echo $sixmonths; ?>">6 months</option>
    <option value="<?php $fivemonths = date('Y-m-d', strtotime('-5 months')); echo $fivemonths; ?>">5 months</option>
    <option value="<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>">4 months</option>
    <option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
    <option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
    <option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
</select>

我希望在选择例如 4 个月时出现一个新的选择标签,如下所示:

<select name="until_date_4months">
    <option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
    <option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
    <option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
    <option value="<?php $now = date('Y-m-d'); echo $now; ?>">Now</option>
</select>

每次选择一个时期(例如 6 个月、5 个月)时,必须出现一个新的选择,其中的选项低于所选的选项加上一个新的选项“现在”,如上所示。这怎么可能?方法是什么?我想通过提交按钮使用这些信息。(使用 JQuery 向特定 PHP 文件发送 GET 请求<-我知道如何处理)。例如,我可以像这样使用 JavaScript:

function sinceDateValue(selection) {
    if (selection.value == "<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>") {
        document.getElementbyId(until_date_4months).style.display = "block"
    }
}

并用于第二次提交id="until_date_4months"style="display: none;". 同样对于第一次提交onchange="testValue(this);,但这只会显示提交,我只想使用表单发送两个信息:since_date 和 until_date...

4

2 回答 2

1

您可以使用循环动态创建选项:

<?php for ($i = 6; $i >= 0; $i--) { ?> 
  <option value="<?php echo date('Y-m-d', strtotime('-' . $i . ' months')); ?>">
    <?php echo ($i == 0) ? 'Now' : $i . ' months'; ?>
  </option>
<?php } ?>

至于 jQuery,您可以根据所选元素获取元素,并使用这些元素填充您的选择框:

$('select[name="since_date"]').on('change', function() {
  var newOpts = $('option:selected', this).nextAll().clone();
  $(newOpts).show().appendTo('#example');   
});

这是一个完整的示例小提琴

于 2013-04-22T15:06:41.810 回答
0

你可以做这样的事情,你需要做的就是设置正确的选项值。

$("#one").change(function(){
    var htmlOf2 = "", d;
    for(var i=1;i<parseInt($(this).find("option:selected").text()) + 1;i++)
   {
       d = new Date();
       d = d.setMonth(d.getMonth() + (i * -1));
       // This sets the right month, but is not set as a value yet. I don't know what format you want

       htmlOf2 += "<option value=''>" + (i + " months") + "</option>";
   }
   htmlOf2 += "<option>NOW</option>";
   $("#two").html(htmlOf2);
}).trigger("change");

现场小提琴:http: //jsfiddle.net/5qaw7/1/

于 2013-04-22T14:52:57.117 回答