jQuery,单选按钮的动态名称更改
寻找类似的东西:
$("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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Ben S 在您最初问题的评论中所写,为什么您需要更改名称?
假设您有
如果用户选择“radioChoice1”并单击提交,浏览器将发送
myRadio=radioChoice1Value 到您的 CGI 脚本。 您可以根据需要进行处理。
如果您坚持更改名称属性,我不确定代码中的“先前”方法是什么。
你能尝试一下,
看看是否有效吗?
我可以想象,如果您尝试更改名称属性,可能会出现跨浏览器问题。
您可以创建隐藏输入并存储用户选择的单选按钮的名称。
编辑:
请发布您的 HTML,以便我可以告诉您什么样的值将发布到 CGI。
例如,如果
用户单击第一个单选按钮“购买”,则在 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
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,
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.
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
If user clicks on the first radio button 'buy', then in your CGI script value for 'transactionType' will be 'buy'.
这是一个完成所请求的工作的 jQuery 脚本。 我用它来重命名表单上的所有 input=text 字段。 虽然我没有对单选或复选框做任何事情,但我添加了相关的案例陈述。 根据需要插入单选/复选框变换。
在 DOM 元素上调用此方法。 我自己正在表单中克隆一个 div。 div 有很多文本字段。 我像这样得到基本元素。
它在 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.
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.
It runs fine in both IE 8.x and Firefox 3.x
我同意其他人的观点,即有一个比重命名输入更好的解决方案。
话虽如此,并回答你原来的问题 - 我建议你将 id 分配给你的无线电输入,然后使用此代码:
我不明白你对 .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:
I don't understand your use of .previous() - I can't find it in the jQuery documentation.