Saya menggunakan kode jQuery untuk membuat html di front end, tetapi saya mendapatkan kesalahan jika kondisi token tidak terduga jika. tolong bantu saya untuk memperbaiki masalah ini, saya kira ada masalah dalam menggabungkan jQuery dengan HTML. Berikut adalah kode saya di bawah ini.

Innerapptemp.blade.php


    for (iS = 0; iS < response.data.stocks.length; ++iS) {
                            stocks += '<tr class="table-row"><td><select> <option> Select </option> <option value="1"' +if(response.data.stocks[iS].color_tag==1) +' selected> Interested </option> <option value="1"' +if(response.data.stocks[iS].color_tag==2) + ' selected> Not Interested </option> </select></td><td>' + response.data.stocks[iS].scan_name + '</td><td>' + response.data.stocks[iS].name + '</td><td>' + response.data.details[response.data.stocks[iS].name].close_price + '</td><td>' + response.data.details[response.data.stocks[iS].name].open_price + '</td><td>' + response.data.details[response.data.stocks[iS].name].last_price + '</td><td>' + response.data.details[response.data.stocks[iS].name].volume + '</td><td>' + response.data.details[response.data.stocks[iS].name].change_from_open + '</td><td>' + response.data.details[response.data.stocks[iS].name].pivot_point + '</td><td>' + response.data.details[response.data.stocks[iS].name].r3 + '</td><td>' + response.data.details[response.data.stocks[iS].name].changefromr3 + '</td><td><button class="btn btn-primary place-btn" attr-order_type="MARKET" attr-product="NRML" attr-symble="{{ $stock->name }}" attr-id="{{ $stock->name }}" data-toggle="modal" data-target="#place-order">Place Order</button></td><td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#depthMain">click</button></td></tr>';
                        }

2
Parteek Kumar 12 Mei 2021, 08:22

1 menjawab

Jawaban Terbaik

Pertama, mari format kode Anda sehingga Anda benar-benar dapat melihatnya:

for (iS = 0; iS < response.data.stocks.length; ++iS) {
    stocks += '<tr class="table-row"><td><select> <option> Select </option> <option value="1"' +
    if(response.data.stocks[iS].color_tag==1) +
    ' selected> Interested </option> <option value="1"' +
    if(response.data.stocks[iS].color_tag==2) +
     ' selected> Not Interested </option> </select></td><td>' +
     response.data.stocks[iS].scan_name +
     '</td><td>' +
     response.data.stocks[iS].name +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].close_price +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].open_price +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].last_price +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].volume +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].change_from_open +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].pivot_point +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].r3 +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].changefromr3 +
     '</td><td><button class="btn btn-primary place-btn" attr-order_type="MARKET" attr-product="NRML" attr-symble="{{ $stock->name }}" attr-id="{{ $stock->name }}" data-toggle="modal" data-target="#place-order">Place Order</button></td><td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#depthMain">click</button></td></tr>';
}

Anda tidak dapat memiliki string panjang dan menggabungkan (+) pernyataan if. Anda harus menyelesaikannya terlebih dahulu dan memasukkannya ke dalam string sebagai variabel, atau memecahnya dan menempatkannya di antara string yang Anda coba buat.

Misalnya, bagian string ini:

 <option value="1"' 
        +if(response.data.stocks[iS].color_tag==1) 
        +' selected> Interested </option> <option value="1"' 
        +if(response.data.stocks[iS].color_tag==2) 
        + ' selected> Not Interested </option> 

Perlu refactored seperti ini:

// this part before you start the big long string
opt1selected = opt2selected =''; 
if(response.data.stocks[iS].color_tag==1) opt1selected=' selected="selected"';
if(response.data.stocks[iS].color_tag==2) opt2selected=' selected="selected"';

   // then in the string
   // .... 
   '<option value="1"' +opt1selected+' </option>' +
    '<option value="2"' +opt2selected+' </option>' +
    

      
0
John Tyner 12 Mei 2021, 06:27