2016-12-31 18 views
1

<td>が空のときに警告ボックスを表示しようとしています。私のコードは以下の通りです:テーブルセルが空のときに警告ボックスを表示

Htmlの

<table id="results"> 
    <Full name: <br> 
    <input id="userInput" type="text" name="fullname"> 
    <br> 
    <input id="submit" type="submit" value="Submit"> 
</table> 

スクリプト

今、警告ボックスがポップアップ表示されませんので、 <td>が提出されたときにどのように私は警告ボックスを表示することができ
var table = $("#results"); 

// get the sparql variables from the 'head' of the data. 
var headerVars = data.head.vars; 

// using the vars, make some table headers and add them to the table; 
var trHeaders = getTableHeaders(headerVars); 
table.append(trHeaders); 

// grab the actual results from the data. 
var bindings = data.results.bindings; 

// for each result, make a table row and add it to the table. 
for (rowIdx in bindings) { 
    table.append(getTableRow(headerVars, bindings[rowIdx])); 
} 

function getTableRow(headerVars, rowData) { 
    var tr = $("<tr></tr>"); 

    for (var i in headerVars) { 
    tr.append(getTableCell(headerVars[i], rowData)); 
    } 

    return tr; 
} 

function getTableCell(fieldName, rowData) { 

        var td = $("<td></td>"); 
        var fieldData = rowData[fieldName]; 

        if ($('td').html().trim() == "") { 
         alert("td is empty"); 
        } 
        else { 
         alert("fieldName = [" + fieldName + "] rowData[fieldName][value] = [" + rowData[fieldName]["value"] + "]"); 
         td.html(fieldData["value"]); 
         return td; 
        } 
        console.log(td); 


       } 

function getTableHeaders(headerVars) { 
    var trHeaders = $("<tr></tr>"); 
    for (var i in headerVars) { 
    trHeaders.append($("<th>" + headerVars[i] + "</th>")); 
    } 
    return trHeaders; 
} 

空ですか?

+0

あなたのアラートは、それはあなたが機能の出てくるリターンで、return文の後で到達不能文で、 –

+0

です。 –

+0

私はそれが助けになるか、何か質問があるかどうかを追加して答えてくれます。 –

答えて

0

$("<td></td>")を実行すると、jQueryはtd要素を作成して返します。したがって、条件td == 0は常にfalseになり、アラートが表示されません。

htmlページでtdを選択するには、$('td')を使用し、要素が空であることを確認するには、.html() jQueryの機能を使用します。

ので、何かのように、

if($('td').html().trim() === ''){ 
    alert('td is empty') 
} 

・ホープ、このことができます。

はあなたのケースでこれを試してみてください:

function getTableCell(fieldName, rowData) { 
    var td = $("<td></td>"); 
    var fieldData = rowData[fieldName]; 
    alert("fieldName = [" + fieldName + "] rowData[fieldName][value] = [" + rowData[fieldName]["value"] + "]"); 

    //You need to check id fieldData["value"] is empty and then trigger alert, as if this empty your td will be empty 
    if (fieldData["value"] && fieldData["value"].trim().length !== 0) { 
    td.html(fieldData["value"]); 
    return td; 
    } else { 
    alert("td is empty"); 
    } 
    console.log(td); 
} 
関連する問題