下拉选项在不应该被选中的情况下被选中
用于选择生日日期的当前代码
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是由于您为
strtotime()
函数提供的格式,当向您提供任何不支持的格式时,将错误的格式或 null 作为参数传递给
strtotime()
建议
您可以这样做,您想要将下拉列表显示为变量并检查它是否为
null
,然后更新您的$年,$日, $month
值,在这种情况下,如果值为空,它将不会选择任何值,而在正确的值上,它将选择正确的选项。This is happening due to the format you are giving to
strtotime()
function, when giving any unsupported format toyou 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 可能无效
http://sandbox.onlinephpfunctions.com/code/bb07478fd5cc5da9535750524631d0c78763fe11
如果你有
或
然后你需要像这样访问生日
所以你的语句需要看起来像这样
如果你没有正确访问字符串,对于任何日期,您都会得到 1970, January, 01。就我而言,由于我处于 GMT+1,因此我得到 1969 年 12 月 31 日作为“0”日期,这是由于传递给 strtotime 的无效日期所致
。或者使用 JavaScript:
Your PHP might be invalid
http://sandbox.onlinephpfunctions.com/code/bb07478fd5cc5da9535750524631d0c78763fe11
If you have
or
then you need to access the birthday like this
So your statements need to look like this
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: