下拉选项在不应该被选中的情况下被选中

发布于 2025-01-11 21:35:39 字数 2275 浏览 0 评论 0原文

用于选择生日日期的当前代码

<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
  <option value="" disabled>Year</option>
  <option value="" disabled>----</option>
...
  <option value="1971" {{date("Y", strtotime($friend->birthday)) == '1971' ? 'selected' : ''}}>1971</option>
  <option value="1970" {{date("Y", strtotime($friend->birthday)) == '1970' ? 'selected' : ''}}>1970</option>
  <option value="1969" {{date("Y", strtotime($friend->birthday)) == '1969' ? 'selected' : ''}}>1969</option>
...
</select>
<select name="dob-month" id="dob-month" class="form-control">
  <option value="" disabled>Month</option>
  <option value="" disabled>-----</option>
  <option value="01" {{date("F", strtotime($friend->birthday)) == 'January' ? 'selected' : ''}}>01</option>
  <option value="02" {{date("F", strtotime($friend->birthday)) == 'February' ? 'selected' : ''}}>02</option>
...
</select>
<select name="dob-day" id="dob-day" class="form-control">
  <option value="" disabled>日</option>
  <option value="" disabled>---</option>
  <option value="01" {{date("d", strtotime($friend->birthday)) == '01' ? 'selected' : ''}}>01</option>
  <option value="02" {{date("d", strtotime($friend->birthday)) == '02' ? 'selected' : ''}}>02</option>
...
</select>

我想要做什么 是添加 {{$friend->birthday == null ? 'selected' : ''}} 如下所示,在每个年/月/日标签中

<select name="dob-year" id="dob-year" class="form-control">
  <option value="" disabled>Year</option>
  <option value="" {{$friend->birthday == null ? 'selected' : ''}} disabled>----</option>
...
</select>

问题 这是一个奇怪的问题,我似乎无法找到导致此问题的问题。 我已经禁用了所有 JavaScript,并仔细检查了从控制器返回的内容,但找不到问题的根源。

即使我在其他选项上硬编码selected,年/月/日的默认选定值始终为 1970/01/01。例如 将仅选择 1970。 如果我删除整个 1970 选项标签,那么一切都会正常工作。因此,为了实现此目的,我必须删除 1970 选项标签、1 月 1 日选项标签和 01 天选项标签。这是没有用的...

有人知道这可能是什么原因造成的吗?

Current code for selecting birthday dates

<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
  <option value="" disabled>Year</option>
  <option value="" disabled>----</option>
...
  <option value="1971" {{date("Y", strtotime($friend->birthday)) == '1971' ? 'selected' : ''}}>1971</option>
  <option value="1970" {{date("Y", strtotime($friend->birthday)) == '1970' ? 'selected' : ''}}>1970</option>
  <option value="1969" {{date("Y", strtotime($friend->birthday)) == '1969' ? 'selected' : ''}}>1969</option>
...
</select>
<select name="dob-month" id="dob-month" class="form-control">
  <option value="" disabled>Month</option>
  <option value="" disabled>-----</option>
  <option value="01" {{date("F", strtotime($friend->birthday)) == 'January' ? 'selected' : ''}}>01</option>
  <option value="02" {{date("F", strtotime($friend->birthday)) == 'February' ? 'selected' : ''}}>02</option>
...
</select>
<select name="dob-day" id="dob-day" class="form-control">
  <option value="" disabled>日</option>
  <option value="" disabled>---</option>
  <option value="01" {{date("d", strtotime($friend->birthday)) == '01' ? 'selected' : ''}}>01</option>
  <option value="02" {{date("d", strtotime($friend->birthday)) == '02' ? 'selected' : ''}}>02</option>
...
</select>

What I want to do
is to add {{$friend->birthday == null ? 'selected' : ''}} like below in each Year/Month/Day tags

<select name="dob-year" id="dob-year" class="form-control">
  <option value="" disabled>Year</option>
  <option value="" {{$friend->birthday == null ? 'selected' : ''}} disabled>----</option>
...
</select>

The problem
This a weird one I cant seem to find the issue that is causing this.
Ive disabled all javascript and double checked what is being returned from controller too but cannot find the source of the issue.

The default selected values of Year/Month/Day is always 1970/01/01 even if I hard code selected on other options. For example <option value="" selected>Year</option> would have it only select 1970.
If I delete the whole 1970 option tag only then everything works fine. So for this to work I had to delete 1970 option tag, 01 January option tag and 01 day option tag. Which is no use...

Does anyone have an idea what could be causing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2025-01-18 21:35:39

发生这种情况是由于您为 strtotime() 函数提供的格式,当向

echo date('Y/d/m',strtotime("20/03/03"));
echo date('Y:d:m',strtotime(null))

it will return you default value which is `1970/01/01`

您提供任何不支持的格式时,将错误的格式或 null 作为参数传递给 strtotime()

建议

您可以这样做,您想要将下拉列表显示为变量并检查它是否为null,然后更新您的$年,$日, $month 值,在这种情况下,如果值为空,它将不会选择任何值,而在正确的值上,它将选择正确的选项。

 @php
    $friendBD =  '';
    $day = '';
    $month = '';
    $year = '';
@endphp


@foreach ($friends as $friend)

    @php
        $friendBD =  $friend->birthday
    @endphp
    
    @if(!is_null($friendBD))
        @php
            $date = \Carbon\Carbon::create($friendBD);
            $month = $date->format('M');
            $day = $date->format('d');
            $year = $date->format('Y');
        @endphp
    @endif


    <label>Birthday</label>
    <select name="dob-year" id="dob-year" class="form-control">
    <option value="" disabled>Year</option>
    <option value="" disabled>----</option>
    <option value="1971" >1971</option>
    <option value="1970" {{$year == '1970' ? 'selected' : ''}}>1970</option>
    <option value="1969" {{$year == '1969' ? 'selected' : ''}}>1969</option>
    <option value="2020" {{$year == '2020' ? 'selected' : ''}}>2020</option>
    </select>
@endforeach

This is happening due to the format you are giving to strtotime() function, when giving any unsupported format to

echo date('Y/d/m',strtotime("20/03/03"));
echo date('Y:d:m',strtotime(null))

it will return you default value which is `1970/01/01`

you are passing either wrong format or null as parameter to strtotime()

Suggestion

You can do something like this, where you want to show the dropdown get dbirthday into a variable and check if it is null or not, and then update your $year, $day, $month value in that case if value is null it will not select any value and on right value it will select the option is correct.

 @php
    $friendBD =  '';
    $day = '';
    $month = '';
    $year = '';
@endphp


@foreach ($friends as $friend)

    @php
        $friendBD =  $friend->birthday
    @endphp
    
    @if(!is_null($friendBD))
        @php
            $date = \Carbon\Carbon::create($friendBD);
            $month = $date->format('M');
            $day = $date->format('d');
            $year = $date->format('Y');
        @endphp
    @endif


    <label>Birthday</label>
    <select name="dob-year" id="dob-year" class="form-control">
    <option value="" disabled>Year</option>
    <option value="" disabled>----</option>
    <option value="1971" >1971</option>
    <option value="1970" {{$year == '1970' ? 'selected' : ''}}>1970</option>
    <option value="1969" {{$year == '1969' ? 'selected' : ''}}>1969</option>
    <option value="2020" {{$year == '2020' ? 'selected' : ''}}>2020</option>
    </select>
@endforeach
还不是爱你 2025-01-18 21:35:39

您的 PHP 可能无效

http://sandbox.onlinephpfunctions.com/code/bb07478fd5cc5da9535750524631d0c78763fe11

如果你有

$friend['birthday'] = "1980/02/01";

$friend = array("birthday"=>"1980/02/01");

然后你需要像这样访问生日

echo strtotime($friend["birthday"]);

所以你的语句需要看起来像这样

echo date("Y", strtotime($friend["birthday"]));

echo date("F", strtotime($friend["birthday"]));

echo date("d", strtotime($friend["birthday"]));

如果你没有正确访问字符串,对于任何日期,您都会得到 1970, January, 01。就我而言,由于我处于 GMT+1,因此我得到 1969 年 12 月 31 日作为“0”日期,这是由于传递给 strtotime 的无效日期所致

。或者使用 JavaScript:

const dob = "1971/02/01" // "<?= $friend["birthday"] ?>";
const [yyyy,mm,dd] = dob.split("/")
document.getElementById("dob-year").value=yyyy;
document.getElementById("dob-month").value=mm;
document.getElementById("dob-day").value=dd;
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
  <option value="" disabled>Year</option>
  <option value="" disabled>----</option>
  <option value="1971">1971</option>
  <option value="1970">1970</option>
  <option value="1969">1969</option>
</select>
<select name="dob-month" id="dob-month" class="form-control">
  <option value="" disabled>Month</option>
  <option value="" disabled>-----</option>
  <option value="01" 01</option>
  <option value="02">02</option>
</select>
<select name="dob-day" id="dob-day" class="form-control">
  <option value="" disabled>日</option>
  <option value="" disabled>---</option>
  <option value="01">01</option>
  <option value="02">02</option>
</select>

Your PHP might be invalid

http://sandbox.onlinephpfunctions.com/code/bb07478fd5cc5da9535750524631d0c78763fe11

If you have

$friend['birthday'] = "1980/02/01";

or

$friend = array("birthday"=>"1980/02/01");

then you need to access the birthday like this

echo strtotime($friend["birthday"]);

So your statements need to look like this

echo date("Y", strtotime($friend["birthday"]));

echo date("F", strtotime($friend["birthday"]));

echo date("d", strtotime($friend["birthday"]));

If you do NOT access the string correctly, your will get 1970, January, 01 for any date. In my case since I am in GMT+1, I get 1969, December, 31 for the "0" date resulting from an invalid date passed to strtotime

Alternatively use JavaScript:

const dob = "1971/02/01" // "<?= $friend["birthday"] ?>";
const [yyyy,mm,dd] = dob.split("/")
document.getElementById("dob-year").value=yyyy;
document.getElementById("dob-month").value=mm;
document.getElementById("dob-day").value=dd;
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
  <option value="" disabled>Year</option>
  <option value="" disabled>----</option>
  <option value="1971">1971</option>
  <option value="1970">1970</option>
  <option value="1969">1969</option>
</select>
<select name="dob-month" id="dob-month" class="form-control">
  <option value="" disabled>Month</option>
  <option value="" disabled>-----</option>
  <option value="01" 01</option>
  <option value="02">02</option>
</select>
<select name="dob-day" id="dob-day" class="form-control">
  <option value="" disabled>日</option>
  <option value="" disabled>---</option>
  <option value="01">01</option>
  <option value="02">02</option>
</select>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文