正如您在http://jsbin.com/erivi/上看到的那样,我正在使用Jquery来更改某些图像(和图像属性),具体取决于所选的单选按钮,当在其中单击时,我还将删除表单文本。
现在,我要尝试的是根据选中的单选按钮更改表单的文本。
例如,当点击“选择3”时,我们应该看到“输入3的文本”。
您可以在此处实时查看和编辑我的代码:http://jsbin.com/erivi/edit
我需要改进的部分是:
imgFldr = 'http://download.kde.org/khotnewstuff/icons/previews/';
$("input[type='radio']").click(function() {
var strarr = this.title.split('|');
if(strarr.length < 2) return;
$('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]).attr('title', starr[2]);
});
特别是
.attr('title', starr[2]);
,它应该根据我的广播标题的第三部分来更改标题(例如:title='m602-1.png|Logo 3|Text of input 3'
)谢谢 :)
编辑:我忘了提及我在真实代码中使用了几种形式,这就是为什么我正在寻找一种无需输入特定文本名称的方式来执行此操作的原因,以避免为其他文本输入重复脚本。
最佳答案
编辑:输入文本框的ID必须是唯一的。此外,您需要在单选按钮的click事件中设置.attr('title',strarr[2])
,.val(strarr[2])
和提示类。还需要对您的inputdynvalue
插件进行一些更改。
请在http://jsbin.com/akawo上查看完整的更新工作代码。
更新的插件代码
(function($) {
$.fn.inputdynvalue = function (options) {
var opts = $.extend({}, $.fn.inputdynvalue.defaults, options);
return this.each(function(){
var hvalue = opts.htext;
switch (opts.htext) {
case 'title' : hvalue = $(this).attr('title'); break;
case 'value' : hvalue = $(this).attr('value'); break;
}
$(this).attr('value', hvalue).addClass(opts.hclass)
.unbind('focus blur')
.bind('focus', function() {
if (this.value === this.title) {
this.value = '';
$(this).removeClass(opts.hclass);
}
})
.bind('blur', function() {
if (this.value === '') {
this.value = this.title;
$(this).addClass(opts.hclass);
}
});
});
};
$.fn.inputdynvalue.defaults = {
htext: 'title',
hclass: 'hint'
};
})(jQuery);
$(document).ready(function(){
$("input[type='text']").inputdynvalue();
});
更新的单选按钮单击事件处理程序
$("input[type='radio']").click(function() {
var strarr = this.title.split('|');
if(strarr.length < 2) return;
$('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]);
$('#'+this.name+'_text')
.attr('title',strarr[2])
.val(strarr[2])
.addClass('hint');
});
关于jquery - 如何根据带有jquery的广播更改某些文本输入的标题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/874096/