从单选按钮获取值以选择2

时间:2017-06-14 08:53:17

标签: javascript jquery jquery-select2

经过很长一段时间的搜索,我仍无法找到问题的解决方案。

func yourFunc(notification : Notification)
{
    switch notification.name
    {
        case NotifyType.userDatabaseUpdated.name: print("Gotcha")
        default: print("Alien")

    }
}

我想将单选按钮值传递给select2元素。

<label class='radio radio-inline'>
            <input type='radio' name='roomtype' value ='1' required @if($row['roomtype'] == '1') checked="checked" @endif > Single Room </label>
            <label class='radio radio-inline'>
            <input type='radio' name='roomtype' value ='2' required @if($row['roomtype'] == '2') checked="checked" @endif > Double Room </label>
            <label class='radio radio-inline'>
            <input type='radio' name='roomtype' value ='3' required @if($row['roomtype'] == '3') checked="checked" @endif > Triple Room </label>

对于maximumSelectionLength值而不是3我想从单选按钮传递值..所以我已经提供了这个解决方案,但它不起作用

$(".select2").select2({ width:"100%", maximumSelectionLength:3 }); 

任何帮助表示赞赏。感谢

编辑: 我的表格是在MODAL中..很遗憾,这些解决方案对我不起作用..

2 个答案:

答案 0 :(得分:0)

您可以使用change回调作为roomtype单选按钮。在回调函数中,您可以通过this.value获取单选按钮的值。因此,您只需将maximumSelectionLength设置为this.value

这样的事情:

$(document).ready(function() {
    $('input[type=radio][name=roomtype]').change(function() {
        alert(this.value);
        // $(".select2").select2({ width:"100%", maximumSelectionLength: this.value }); // uncomment this
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class='radio radio-inline'>
   <input type='radio' name='roomtype' value ='1'  checked="checked"> Single Room </label>
<label class='radio radio-inline'>
   <input type='radio' name='roomtype' value ='2'> Double Room </label>
<label class='radio radio-inline'>
   <input type='radio' name='roomtype' value ='3'> Triple Room </label>

答案 1 :(得分:0)

另一种方法可能是简单地使用Laraval Blade来设置private refreshGui(data: {}){ this._node.data = data; this.objectProperties = new Array(); this.nodeChildren = new Array(); for (var property in data) { var propertyValue = data[property]; if (propertyValue instanceof Array) { this.nodeChildren.push({label: property, value: "total: ".concat(propertyValue.length.toString())}); } else { this.objectProperties.push({label: property, value: propertyValue}); } } } (我假设您在代码中使用了@if指令中的Laravel Blade)。

maximumSelectionLength