Firefox 中的 jquery.change()
每当用户更改月份时,我都会使用 jQuery 更改来更改另一个选择下拉列表的值(以便显示正确的天数)。这在除 Firefox 之外的所有浏览器中都非常有效:(
可以说代码是这样的大
$(document).ready(function() {
var leap;
$('.dob').change(function() {
var y = $('#ryear option:selected').html(); /* The year selected */
var s = $('#rmonth option:selected').html(); /* The month selected */
});
});
然后我根据变量值更改数据。 有 3 个带有 .dob 的选择,所以有点像这样
<select class="dob" id="rday">
<option id="01">01</option>
....
</select>
<select class="dob" id="rmonth">
<option id="1876">Jan</option>
....
</select>
<select class="dob" id="ryear">
<option id="1876">1876</option>
....
</select>
在 Firefox 中,当我选择月份或年份选择下拉列表时(脚本并未真正选择日期的值,因此它不受影响),好吧,下拉列表闪烁出现,然后在 Firefox 中单击一下立即消失。
有什么想法为什么脚本要这样做吗?
I'm using the jQuery change to change the values of another select drop down whenever the user changes the month (so that the right number of days is displayed). This works great in all browsers except Firefox :(
Suffice to say the code is a big like this
$(document).ready(function() {
var leap;
$('.dob').change(function() {
var y = $('#ryear option:selected').html(); /* The year selected */
var s = $('#rmonth option:selected').html(); /* The month selected */
});
});
Then I alter the data with the variable values in mind.
There are 3 selects with .dob, so its a bit like this
<select class="dob" id="rday">
<option id="01">01</option>
....
</select>
<select class="dob" id="rmonth">
<option id="1876">Jan</option>
....
</select>
<select class="dob" id="ryear">
<option id="1876">1876</option>
....
</select>
In firefox when I select the month or year select drop down (the value of the day isnt really selected by the script so it isnt affected), well, the drop down flashes and appears, and then instantly disappears on a single click in Firefox.
Any ideas why the script is doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ids 不能以数字开头。
id 必须是唯一标识符。
ids can not start with numbers.
ids have to be UNIQUE identifiers.
我相信这与数据类型有关,而不是 var y = $('#ryear option:selected').html();
尝试这个
var y = $('#ryear option:selected').val();
||var y = $('#ryear option:selected').attr('id');
||parseInt(y);
I believe it is to do with data type, Instead of
var y = $('#ryear option:selected').html();
try this
var y = $('#ryear option:selected').val();
||var y = $('#ryear option:selected').attr('id');
||parseInt(y);
我建议首先重写代码以符合标准,这意味着用值替换 ID,即
(顺便说一句,正如所指出的,ID 不能以数字 http://www.w3schools 开头。 com/tags/att_standard_id.asp)
此外,当您获取值时,请使用
.val()
,因此通过这些修复,看看您是否仍然遇到问题。
另外,我发现,如果您的选择处理函数“强制”在另一个选择框中选择值(该选择框也具有相同的选择处理程序),则可能会出现某种干扰。我建议至少出于测试目的,为每个选择分配唯一的 id 并为每个选择创建唯一的函数。
I would recommend rewriting the code first to be standards compliant, which means replacing IDs with values, i.e.
<option value="1876">
(BTW IDs, as pointed out, can't start with a digit http://www.w3schools.com/tags/att_standard_id.asp)
Also, when you're getting the value use
.val()
, soWith those fixes see if you still get the problem.
Also, it occurs to me that if your select handing function "forces" value selection in another select box, which also has the same select handler, there could be interference of some sort. I would recommend, at least for testing purposes, to assign unique id to each select and create unique functions for each.