Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何使用单选按钮的值选择单选按钮_Jquery_Html - Fatal编程技术网

Jquery 如何使用单选按钮的值选择单选按钮

Jquery 如何使用单选按钮的值选择单选按钮,jquery,html,Jquery,Html,在我的代码中,我为所有单选按钮使用了一组具有相同id和名称的单选按钮。。但它的价值是不同的 我有我想要选择的单选按钮的值。。如何使用我的值选择该单选按钮。 考虑一个例子… <input name="newColorCode" id="newColorCode" type="radio" value="color_code_LightViolet"/> <input name="newColorCode" id="newColorCode" type="radio" value

在我的代码中,我为所有单选按钮使用了一组具有相同id和名称的单选按钮。。但它的价值是不同的

我有我想要选择的单选按钮的值。。如何使用我的值选择该单选按钮。 考虑一个例子…

 <input name="newColorCode" id="newColorCode" type="radio" value="color_code_LightViolet"/>
<input name="newColorCode" id="newColorCode" type="radio" value="color_code_red"/>
<input name="newColorCode" id="newColorCode" type="radio" value="color_code_green"/>

但是它不起作用

首先,ID在整个DOM中必须是唯一的。因此,拥有3台带有
id=“newColorCode”
的收音机是错误的。因此,首先删除这些id属性,或者为每个按钮提供唯一的属性。然后回答你的问题:

$(':radio[value="color_code_green"]').attr('checked', 'checked');
这里有一个例子


代码无法工作的原因是,
$(..)
函数返回一个jQuery包装的DOM元素数组,而您试图设置的
checked
属性只是没有定义。因此,您可以使用该函数对选择器返回的所有元素执行此操作。

您也可以在JavaScript中执行此操作:

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function
函数getSelectedRadio(按钮组){
//返回所选单选按钮的阵列号,如果未选择任何按钮,则返回-1
if(buttonGroup[0]){//如果按钮组是数组(一个按钮不是数组)
对于(var i=0;i
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function