2016-06-29 2 views
1

私はWebアプリケーションにLaravelを使用しています。ブートストラップモードの動的URL

「国」テーブルの各行には、モーダルを開き、テキストエリアに既存の国名を入力する「編集」ボタンがあります。このテキストエリアは、データベースで編集および更新できます。

これは私のモーダルの部分的なコードです:私がしたいことを同じスクリプトで

$(function() { 
    $('#editModal').on("show.bs.modal", function (e) { 
     $("#lblCountryId").html($(e.relatedTarget).data('id')); 
     document.getElementById("txtCountryName").value = $(e.relatedTarget).data('name'); 
    }); 
}); 

{!! Form::model($country, [ 
    'method' => 'PATCH', 
    'url' => ['country', 'lblCountryId'], 
    'class' => 'form-horizontal' 
]) !!} 

// some more code 

// and the edit button for each row 
<td> 
    <button type="submit" class="btn btn-primary btn-xs" 
     data-toggle="modal" data-target="#editModal" 
     data-id=" {{ $item->id }}" 
     data-name="{{ $item->name }}"> 
     <i class="fa fa-pencil" aria-hidden="true"></i> 
    </button> 
</td> 

モーダルショー、私はJSをfolloringとテキストフィールドの値をmanupulateフォームが投稿しているURLを編集しますが、JSはそれを好きではありません。

クリックした行に応じてフォームのURLを動的に変更する方法はありますか?

+0

フォームのターゲットURLを変更するコードを表示します。 –

+0

"show.bs.modal"の代わりに "shown.bs.modal"を試しましたか? – Abhishek

答えて

0

私の形式:

{!! Form::model($country, [ 
     'method' => 'PATCH', 
     'class' => 'form-horizontal', 
     'name' => 'myEditForm' 
]) !!} 

<td> 
    <button type="submit" class="btn btn-primary btn-xs" 
     data-toggle="modal" data-target="#editModal" 
     data-id=" {{ $item->id }}" 
     data-name="{{ $item->name }}"> 
     <i class="fa fa-pencil" aria-hidden="true"></i> 
    </button> 
</td> 

とJS:

<script type="text/javascript"> 
$(function() { 
    $('#editModal').on("show.bs.modal", function (e) { 
     document.getElementById("txtCountryName").value = $(e.relatedTarget).data('name'); 
     document.myEditForm.action = "{{ url('country') }}" + '/' + $(e.relatedTarget).data('id'); 
    }); 
}); 
</script> 
0

変更し、これにフォーム:

{!! Form::model($country, [ 
    'method' => 'PATCH', 
    'class' => 'form-horizontal', 
    'id' => 'myForm' 
]) !!} 

そして、あなたのスクリプトロークこの:

に:私はこのようにそれを解決することができたpreviuous回答の助けを借りて

$(function() { 
    $('#editModal').on("show.bs.modal", function (e) { 
     var url = "{!! route('country', ':_data_id') !!}"; 
     $('#myForm').attr('action', url.replace(':_data_id', e.relatedTarget.data('id'))); 
     document.getElementById("txtCountryName").value = $(e.relatedTarget).data('name'); 
    }); 
}); 
+0

ありがとう!それはよさそうだ。 私はいくつかの問題を抱えています。 Laravelは好きではないようです。 'var url =" {!! route( 'country'、 ':_data_id')!!} "; ' - >経路[国]が定義されていません ' var url = "{!! route( 'country.edit'、 ':_data_id')!!}"; 'に変更すると、モーダルは存在しませんまったく表示。 –

+0

(show.bs.modal、function(e))をon( "shown.bs.modal"、function(e))に変更してください。country.editルートを教えてください。 –

+0

モーダルが示すように、編集ボタンをクリックした行に関係なく、同じ「名前」を表示します。 –