在jquery中按值选择选项

在jquery中按值选择选项,jquery,Jquery,我有4个选择: <input type="radio" name="tag" value="1"/> <input type="radio" name="tag" value="2"/> <input type="radio" name="tag" value="3"/> <input type="radio" name="tag" value="4"/> 请告诉我,当我点击上一个按钮时,应该选择已经选择的选项,这是怎么可能的。我应该在该函数中添

我有4个选择:

<input type="radio" name="tag" value="1"/>
<input type="radio" name="tag" value="2"/>
<input type="radio" name="tag" value="3"/>
<input type="radio" name="tag" value="4"/>
请告诉我,当我点击上一个按钮时,应该选择已经选择的选项,这是怎么可能的。我应该在该函数中添加什么来执行此操作?

尝试以下操作:

$('input:radio[name="tag"][value="' + option_selected + '"]').prop('checked', true);
$('input:radio[name="tag"][value="' + option_selected + '"]').prop('checked', true);
阅读有关

一点解释:
$('input:radio[name=“tag”][value=“”+option_selected+”)
使用
name=tag选择
input[type=radio]
并且选择了
等于
选项

您可以试试


如何存储以前选择的值?它存储在数据库中,由ajax调用变量“option_selected”检索@fawad:所选的
选项的内容是否与单选按钮的
属性完全对应?是的,变量中的值完全相同是的,但再次按下上一页按钮时,它没有选择选项:(那么,在你的情况下,有些事情并不是你想象的那样。试着用这把小提琴来演示:@fawad你没有得到一个正确的
option\u选择的
变量。试着
alert(option\u选择的)
查看设置内容。选项select变量通过警报功能显示正确的选项使用
input:radio
避免对选择器进行全局DOM搜索。有关
input:radio
的详细信息,请参阅我的答案。现在请小心。您不想让选择器变得如此复杂,因为这将花费大量开销。简单的解决方案:在每个输入中添加一个类,并将该类用作选择器。这样看起来也会更整洁。
$('input:radio[name="tag"][value="' + option_selected + '"]').prop('checked', true);
$('input:[name="tag"],[name="radio"],[value="' + option_selected + '"]').prop('checked', true);