最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery, dynamic name change of radio button - Stack Overflow

programmeradmin4浏览0评论

Looking for something like:

$("input:radio:checked").previous("name", "original_name").attr("name","new_name");

I tried a few different seen around here but mostly get the error: Object Expected

Any help is appreciated.

Looking for something like:

$("input:radio:checked").previous("name", "original_name").attr("name","new_name");

I tried a few different seen around here but mostly get the error: Object Expected

Any help is appreciated.

Share Improve this question edited Jul 13, 2009 at 19:34 T.T.T. asked Jul 13, 2009 at 19:23 T.T.T.T.T.T. 34.6k47 gold badges134 silver badges172 bronze badges 8
  • 4 Mind if I ask why you'd like to dynamically change the name of a radio button? – Ben S Commented Jul 13, 2009 at 19:26
  • I have two different actions available for the 1 selected input. – T.T.T. Commented Jul 13, 2009 at 19:27
  • I fail to see how that requires renaming a radio button. – Ben S Commented Jul 13, 2009 at 19:30
  • The input is being sent to a cgi. I need to distinguish the name of the radio button selected with a different submit, for different functionality, don't want more radio buttons on the UI. – T.T.T. Commented Jul 13, 2009 at 19:33
  • 1 Why don't you just check to see if the radio button is checked in the cgi? If was checked, then its name would have changed, you're in the same state. Also, what if your users aren't using javascript? Then your page breaks. – Ben S Commented Jul 13, 2009 at 19:38
 |  Show 3 more ments

3 Answers 3

Reset to default 7

As Ben S wrote in the ment to your original question, why do you need to change the name?

Let's say you have

<input type="radio" name="myRadio" id="radioChoice1" value="radioChoice1Value" />
<input type="radio" name="myRadio" id="radioChoice2" value="radioChoice2Value" />

If user selects 'radioChoice1' and clicks submit, browser will send

myRadio=radioChoice1Value to your CGI script. You can process this as you desire.

In case you insist on changing the name attribute, I am not sure what is the 'previous' method in your code.

Can you try,

$("input:radio:checked").attr("name","new_name");

And see if it works?

I can imagine that there could be cross browser problems if you try to change the name attribute. You can create a hidden input and store the name of the radio button selected by the user.

<input type="hidden" id="selectedRadioButton" />

$('#selectedRadioButton').val($("input:radio:checked").attr('name'));

EDIT:

Please post your HTML so that I can tell you what kind of values will be posted to CGI.

For example if you have

<input type="radio" name="transactionType" id="buy" value="buy" />
<input type="radio" name="transactionType" id="sell" value="sell" />

If user clicks on the first radio button 'buy', then in your CGI script value for 'transactionType' will be 'buy'.

Here's a jQuery script that does the job requested. I'm using it to rename all the input=text fields on a form. Though I'm not doing anything with radio or checkbox, I added the relevant case statements. Insert your radio/checkbox transforms as needed.

    function renameTextInputFields(ele) {
        var i = 0;
        $(ele).find(':input').each(function() {
            switch(this.type) {
                case 'text':
                    $(this).attr("name","textFieldNumber"+(++i));
                    break;
                case 'checkbox':
                case 'radio':
            });
    }

Call this method on a DOM element. Myself, I'm making a clone of a div within a form. The div has lots of text fields. I get the base element like so.

var clone = $('.classAssignedToMyDiv').clone(); 

It runs fine in both IE 8.x and Firefox 3.x

I agree with the others that there is a better solution than to rename the input.

Having said that, and to answer your original question - I suggest that you assign ids to your radio inputs, and then use this code:

$("#inputId").attr("name","new_name");

I don't understand your use of .previous() - I can't find it in the jQuery documentation.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论