我很好奇整合这堆Jquery .focus函数的最好方法是什么。
基本上,我使用它来允许URL具有#ID,当它这样做时,Jquery会更新页面上显示的表单。
最好的方法是合并它,这样我就不需要一遍又一遍地重复相同的代码了?
注意,所有带有ID的元素都有相同的类。
谢谢,J
--
$('#select-1').focus(function(e) {
// set the selected choice based on URL ID
$category_select.val('1');
updateSupport($categorychoice.val());
});
$('#select-2').focus(function(e) {
// set the selected choice based on URL ID
$category_select.val('billing');
updateSupport($categorychoice.val());
});
$('#select-3').focus(function(e) {
// set the selected choice based on URL ID
$category_select.val('setup');
updateSupport($categorychoice.val());
});
$('#select-4').focus(function(e) {
// set the selected choice based on URL ID
$category_select.val('errors');
updateSupport($categorychoice.val());
});
$('#select-5').focus(function(e) {
// set the selected choice based on URL ID
$category_select.val('customization');
updateSupport($categorychoice.val());
});
$('#select-6').focus(function(e) {
// set the selected choice based on URL ID
$category_select.val('6');
updateSupport($categorychoice.val());
});发布于 2015-02-09 20:14:58
为什么不给每个'select-?‘添加一个属性(比如data-source-id="item id")呢?元素之后,您可以使用css选择器附加焦点()和属性,以确定它与哪个id相关……
$(".myCssName").focus(....)
$category_select.val( $(this).data("source-id") )建议由kpblc编辑
发布于 2015-02-10 20:58:14
尝试使用对象:
var values = {
'select-1' : '1',
'select-2' : 'billing',
'select-3' : 'setup',
'select-4' : 'errors',
'select-5' : 'customization',
'select-6' : '6'
};
$('[id^=select-]').on('focus', function(e) {
$category_select.val(values[this.id]);
updateSupport($categorychoice.val());
});https://stackoverflow.com/questions/28409408
复制相似问题