如何使用jquery根据收音机更改某些文本输入的标题?

发布于 2024-07-20 19:52:04 字数 910 浏览 3 评论 0原文

正如您在 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|输入文本 3' )

谢谢:)

编辑:我忘了提及我在真实代码中使用了多种表单,这就是为什么我正在寻找一种方法这没有文本输入的特定名称,以避免为其他文本输入重复脚本。

As you can see on http://jsbin.com/erivi/ I'm using Jquery to change some image (and image attribute) depending on radio button selected, I'm also removing form text when clicking inside it.

And now what I'm trying to do is to change the text of the form depending on the radio button checked.

For example when clicking on "Choice 3" we should see "Text of input 3".

You can see & edit my code live here: http://jsbin.com/erivi/edit

The part I need to improve is:

  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]); 
}); 

And particularly .attr('title', starr[2]); which is supposed to change title depending on the 3rd part of my radio title (exemple: title='m602-1.png|Logo 3|Text of input 3' )

Thanks :)

Edit: I forgot to mention that I'm using several forms on my real code, that's why I'm looking for a way to do this without the particular name of the text input in order to avoid repeating script for other text input.

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

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

发布评论

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

评论(2

小…楫夜泊 2024-07-27 19:52:04

编辑: 输入文本框的 ID 必须是唯一的。 此外,您还需要在单选按钮的单击内设置 .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'); 
}); 

EDIT: The id of the input textbox has to be unique. Additionally, you need to set .attr('title',strarr[2]), .val(strarr[2]) and the hint class inside the radio button's click event. Also some changes need to be made to your inputdynvalue plugin.

Please see the complete updated working code at http://jsbin.com/akawo

Updated Plug-in code

(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(); 
});

Updated Radio Button Click Event Handler

$("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'); 
}); 
烟─花易冷 2024-07-27 19:52:04

尝试这个:

<form method="post" action="">
    <div>
        <img id="iymok" src="http://download.kde.org/khotnewstuff/icons/previews/m732-1.png" alt="Alt by default" />
        <input type="text" id="iymok_text" title="Blablabla Text 1" />
        <label><input type="radio" checked="checked" name="iymok" title="m133-1.png|Logo 1|Blablabla Text 1" />Choice 1</label>
        <label><input type="radio" name="iymok" title='m203-1.png|Logo 2|Text of input 2' />Choice 2</label>
        <label><input type="radio" name="iymok" title='m602-1.png|Logo 3|Text of input 3' />Choice 3</label>
    </div>
</form>
<script type="text/javascript">
    imgFldr = 'http://download.kde.org/khotnewstuff/icons/previews/';
    $("input[type='radio']").click(function() {
        var strarr = this.title.split('|');
        if (strarr.length >= 3) {
               $('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]).attr('title', strarr[2]);
               $('#'+this.name+'_text').attr('title', strarr[2]);
        }
    });
</script>

Try this:

<form method="post" action="">
    <div>
        <img id="iymok" src="http://download.kde.org/khotnewstuff/icons/previews/m732-1.png" alt="Alt by default" />
        <input type="text" id="iymok_text" title="Blablabla Text 1" />
        <label><input type="radio" checked="checked" name="iymok" title="m133-1.png|Logo 1|Blablabla Text 1" />Choice 1</label>
        <label><input type="radio" name="iymok" title='m203-1.png|Logo 2|Text of input 2' />Choice 2</label>
        <label><input type="radio" name="iymok" title='m602-1.png|Logo 3|Text of input 3' />Choice 3</label>
    </div>
</form>
<script type="text/javascript">
    imgFldr = 'http://download.kde.org/khotnewstuff/icons/previews/';
    $("input[type='radio']").click(function() {
        var strarr = this.title.split('|');
        if (strarr.length >= 3) {
               $('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]).attr('title', strarr[2]);
               $('#'+this.name+'_text').attr('title', strarr[2]);
        }
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文