jQuery,单选按钮的动态名称更改

发布于 2024-07-26 09:13:08 字数 181 浏览 1 评论 0原文

寻找类似的东西:

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

我尝试了这里看到的一些不同的东西,但大多得到错误:预期对象

任何帮助表示赞赏。

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.

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

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

发布评论

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

评论(3

倒带 2024-08-02 09:13:08

正如 Ben S 在您最初问题的评论中所写,为什么您需要更改名称?

假设您有

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

如果用户选择“radioChoice1”并单击提交,浏览器将发送

myRadio=radioChoice1Value 到您的 CGI 脚本。 您可以根据需要进行处理。

如果您坚持更改名称属性,我不确定代码中的“先前”方法是什么。

你能尝试一下,

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

看看是否有效吗?

我可以想象,如果您尝试更改名称属性,可能会出现跨浏览器问题。
您可以创建隐藏输入并存储用户选择的单选按钮的名称。

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

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

编辑:

请发布您的 HTML,以便我可以告诉您什么样的值将发布到 CGI。

例如,如果

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

用户单击第一个单选按钮“购买”,则在 CGI 脚本中“transactionType”的值将为“购买”。

As Ben S wrote in the comment 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'.

花海 2024-08-02 09:13:08

这是一个完成所请求的工作的 jQuery 脚本。 我用它来重命名表单上的所有 input=text 字段。 虽然我没有对单选或复选框做任何事情,但我添加了相关的案例陈述。 根据需要插入单选/复选框变换。

    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':
            });
    }

在 DOM 元素上调用此方法。 我自己正在表单中克隆一个 div。 div 有很多文本字段。 我像这样得到基本元素。

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

它在 IE 8.x 和 Firefox 3.x 中运行良好

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

骷髅 2024-08-02 09:13:08

我同意其他人的观点,即有一个比重命名输入更好的解决方案。

话虽如此,并回答你原来的问题 - 我建议你将 id 分配给你的无线电输入,然后使用此代码:

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

我不明白你对 .previous() 的使用 - 我可以在 jQuery 文档中没有找到它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文