Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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,如何使用jQuery更改单选按钮的复选框?var checkbox=$(“#myCheckbox”); var checkbox = $("#myCheckbox"); checkbox.replaceWith('<input type="radio" name="'+checkbox.attr('name')+'" value="'+checkbox.attr('value')+'" />'); 复选框。替换为(“”); 或者使用jQuery1.4 var checkbox =

如何使用jQuery更改单选按钮的复选框?

var checkbox=$(“#myCheckbox”);
var checkbox = $("#myCheckbox");
checkbox.replaceWith('<input type="radio" name="'+checkbox.attr('name')+'" value="'+checkbox.attr('value')+'" />');
复选框。替换为(“”);
或者使用jQuery1.4

var checkbox = $("#myCheckbox");
$("<input>",{
    type:'radio',
    name: checkbox.attr('name'),
    value: checkbox.attr('value')
}).replace(checkbox);
var checkbox=$(“#myCheckbox”);
$("",{
类型:'无线电',
名称:checkbox.attr('name'),
值:checkbox.attr('value')
}).替换(复选框);

您更改type属性是因为它会导致IE出现问题。

我认为您无法更改type属性

作为替代方案,您可以使用div:一个用于复选框,另一个用于收音机。并使用
show()
/
hide()
方法…

给定:

<input type="checkbox" name="change" id="change">
<label for="changem">Change Buttons</label><br><br>
<div id="btngroup">
<span><input type="radio" name="btngroup" id="btn-1"></span>
<label for="btn-1">Button 1</label><br>
<span><input type="radio" name="btngroup" id="btn-2"></span>
<label for="btn-2">Button 2</label><br>
<span><input type="radio" name="btngroup" id="btn-3"></span>
<label for="btn-3">Button 3</label><br>
</div>

更改按钮

按钮1
按钮2
按钮3
jQuery:

$(document).ready(function() {
  $('#change').click(function() {
    if( $('#change:checked').length > 0 ) {
      var myType = "checkbox";
    }  else {
      var myType = "radio";
    }
     $("#btngroup span").each(function(i) {
     $(this).html('<input type="' + myType + '" name="btngroup" id="btn-' + (i+1) + '">');
    });
  });
});
$(文档).ready(函数(){
$(“#更改”)。单击(函数(){
如果($('#更改:选中')。长度>0){
var myType=“checkbox”;
}否则{
var myType=“无线电”;
}
$(“#b组跨度”)。每个(函数(i){
$(this.html(“”);
});
});
});

这当然是可能的。你需要它做什么,在什么样的场景中?我有一个单选按钮,我需要它,如果值是其中一个,5个复选框在5个单选按钮中转换,反之亦然,我希望你能理解。我将要键入的内容大致如下:)我不知道这在jQuery 1.4中是否有效,但在1.6中不起作用。改为使用:
var checkbox=$(“#myCheckbox”);替换为($(“”,{type:'radio',name:checkbox.attr('name'),value:checkbox.attr('value'))})