簡體   English   中英

Laravel背包依賴文本框

[英]Laravel backpack dependent textbox

我被Laravel背包困住了。

我想在單擊復選框時顯示文本框。 我想在我的項目中實現這一點。 如何使用控制器實現這一點? 我的字段在控制器中初始化

Controller.php這樣

  // *****
        // FIELDS ALTERNATIVE
        // *****
        "fields" => [

            [
                'name' => "event_name",
                'label' => "Event name",
                'type' => "text",
                'placeholder' => "First Name and Last Name",
            ],
            [
                'name' => "event_topic",
                'label' => "Event Topic",
                'type' => "text",
                'placeholder' => "Event Topic",
            ],
            [
                'name' => "event_type_id",
                'label' => "Event Type",
                'model' => "App\Larapen\Models\EventType",
                'entity' => "eventType",
                'attribute' => "name",
                'type' => "select",
            ],

           [   // Checkbox
            'name' => 'social_links',
            'label' => 'Links to facebook and twitter',
            'type' => 'checkbox'
        ],
    ]

當我點擊“social_links”復選框時,我的添加頁面中應顯示兩個文本框。

請幫幫我。等待回復...........

你可以這樣做:

 var checked = document.getElementById('checkbox'); checked.addEventListener('change', function() { if (this.checked){ document.getElementById('text_1').style.display='block'; document.getElementById('text_2').style.display='block'; } else { document.getElementById('text_1').style.display='none'; document.getElementById('text_2').style.display='none'; } }); 
 <input type="checkbox" id="checkbox" aria-label="..."> <textarea rows="4" cols="50" id="text_1" style="display:none"> Text Area 1 </textarea> <textarea rows="4" cols="50" id="text_2" style="display:none"> Text Area 2 </textarea> <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script> 

您需要在字段中添加一些屬性:

$this->crud->addFields([
        [
            'name' => "event_name",
            'label' => "Event name",
            'type' => "text",
            'placeholder' => "First Name and Last Name",
            'attributes' => ['id'=>'text_1', 'style' => 'display:none'],
        ],
        [
            'name' => "event_topic",
            'label' => "Event Topic",
            'type' => "text",
            'placeholder' => "Event Topic",
            'attributes' => ['id'=>'text_2', 'style' => 'display:none'],
        ],
        [   // Checkbox
            'name' => 'social_links',
            'label' => 'Links to facebook and twitter',
            'type' => 'checkbox',
            'attributes' => ['id'=>'checkbox'],
        ],
]);

我剛剛對Marco提供的答案稍作修改。

           [   
                'name' => 'social_links',
                'label' => 'Links to facebook and twitter',
                'type' => 'checkbox',
                'id'=>'checkbox'
            ],
            [
                'name' => "event_name",
                'label' => "Event name",
                'type' => "text",
                'placeholder' => "First Name and Last Name",
                'id'=>'text_1',
            ],
            [
                'name' => "event_topic",
                'label' => "Event Topic",
                'type' => "text",
                'placeholder' => "Event Topic",
                'id'=>'text_2'
            ],

在視圖頁面中

<script>
$( document ).ready(function() {
    $('#text_1').parent().hide();
    $('#text_2').parent().hide();

});

var checked = document.getElementById('checkbox');
checked.addEventListener('change', function() {
  if (this.checked){
    $('#text_1').parent().show();
    $('#text_2').parent().show();
  } else {
    $('#text_1').parent().hide();
    $('#text_2').parent().hide();
  }
});
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM