简体   繁体   English

使用javascript将选择更改为复选框

[英]Change select to check boxes using javascript

The reason I would like to do this, is that it will be situational. 我想这样做的原因是,这将视情况而定。 If a user is logged in, they will see drop downs. 如果用户已登录,他们将看到下拉列表。 If not, they will see a list of text. 如果没有,他们将看到文本列表。 Ideally just plain text, but I don't know if that's possible, so I was thinking I could convert the <select> to checkboxes and hide the check boxes with CSS. 理想情况下,只是纯文本,但我不知道是否可行,因此我想将<select>转换为复选框,并使用CSS隐藏复选框。

Basically we don't want a user who isn't logged in to feel they can select anything, because they won't be able to order and this could lead to frustration. 基本上,我们不希望未登录的用户感到他们可以选择任何东西,因为他们将无法订购,这可能会导致沮丧。 But we would still like them to view what options are available for each product as unselectable text. 但是我们仍然希望他们以不可选择的文本形式查看每种产品的可用选项。 If there is a better way to do this than what I'm thinking, I'd be grateful for suggestions. 如果有比我想的更好的方法,我将不胜感激。 For now, this is what I've patched together, but it's not changing the select to checkboxes. 目前,这是我已修补的内容,但未将选择更改为复选框。

I grabbed some code from here to use as a starting point: http://www.w3schools.com/jsref/met_element_setattribute.asp 我从这里获取了一些代码作为起点: http : //www.w3schools.com/jsref/met_element_setattribute.asp

Also, I can't grab the < select > by id, because this will be on all < select >'s. 另外,我无法通过ID来获取<select>,因为这将出现在所有<select>上。

<select id="unique_id" class="unique_class"  data-attribute_name="unique_attribute_name" name="unique_name">
    <option value="" selected="selected">Choose an option</option>
    <option class="attached enabled" value="516">5/16"</option>
    <option class="attached enabled" value="38">3/8"</option>
</select>

Javascript: Javascript:

function myFunction() {
    document.getElementsByTagName("SELECT")[0].setAttribute("type", "checkbox"); 
}

Here is a fiddle: https://jsfiddle.net/d4qdekom/ 这是一个小提琴: https : //jsfiddle.net/d4qdekom/

If I am understanding your goal correctly, you can just put div's in and hide/show or enable/disable the div's depending on if the user is logged in or not. 如果我正确地理解了您的目标,则可以根据用户是否登录来放置div并隐藏/显示或启用/禁用div。 So wrap your select up in a div and just toggle that. 因此,将选择内容包装在div中,然后进行切换即可。

This would avoid trying to change the html by simply showing/hiding or enabling/disabling what you want the user to see. 这样可以避免仅通过显示/隐藏或启用/禁用您希望用户看到的内容来尝试更改html的方法。

This should convert your select to checkboxes, provided there is an HTML element with an id of "container" to append the new checkboxes. 如果存在一个ID为“容器”的HTML元素来追加新的复选框,这会将您的选择转换为复选框。

This is the HTML 这是HTML

<select id="unique_id" class="unique_class" data-attribute_name="unique_attribute_name" name="unique_name">
    <option value="" selected="selected">Choose an option</option>
    <option class="attached enabled" value="516">5/16"</option>
    <option class="attached enabled" value="38">3/8"</option>
</select>

<div id="container"></div>

The JavaScript function to transform the select to checkboxes below JavaScript函数将下面的选择转换为复选框

function myFunction() {
    var select = document.getElementById('unique_id');
    var container = document.getElementById('container'); 
    var i;
    for (i = 0; i < select.length; i++) {
        var checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.name = 'option';
        checkbox.id = 'randomId' + i;
        checkbox.value = select.options[i].text;
        var label = document.createElement('label')
        label.htmlFor = 'randomId' + i;
        label.appendChild(document.createTextNode(select.options[i].text));
        container.appendChild(checkbox);
        container.appendChild(label);
    }
}

You can then go ahead and remove the select from the DOM. 然后,您可以继续进行操作,然后从DOM中删除所选内容。 I guess all these should be made easier with JQuery 我想所有这些都应该通过JQuery简化

This is an update using getElementsByTagName: 这是使用getElementsByTagName的更新:

var selects = x.getElementsByTagName('SELECT');  // this is a node list
for (var i = 0; i < y.length; i++) {
  var select = selects[i]; // this is a single select element
  // You can call the above function on each select here...
}

This is a working jsFiddle 这是一个工作的jsFiddle

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM