0

Ajax を使用して Label(asp) から値を読み取ろうとしています。しかし、私は常にUndefindを取得しています:|

私のコードは次のとおりです。

function NIS2USD() {
    var from = document.getElementById("NIS").value;
    var to = document.getElementById("USD").value;
    var amount = document.getElementById("totalAmountLabel").value;
    request = new XMLHttpRequest();
    request.onreadystatechange = ProcessResponse;
    request.open("GET", "Convert.aspx?from=" + num1 + "&to=" + num2 + "&amount=" +    amount, true);
    request.send();
}
function USD2NIS() {
    var from = document.getElementById("USD").value;
    var to = document.getElementById("NIS").value;
    var amount = document.getElementById("totalAmountLabel").value;
    request = new XMLHttpRequest();
    request.onreadystatechange = ProcessResponse;
    request.open("GET", "Convert.aspx?from=" + num1 + "&to=" + num2 +  "&amount="+amount, true);
    request.send();
}
function ProcessResponse() {
    if (request.readyState == 4 && request.status == 200) {
        document.getElementById("totalAmountLabel").innerHTML = request.responseText;
    }
}

ラベルの私の定義は次のとおりです。

<asp:Label ID="totalAmountLabel" runat="server" Text="Label"></asp:Label>

なぜ私はいつも undef になるのですか?

4

4 に答える 4

1

これは、更新パネル内でコントロールの名前が変更されるためです。jQueryリファレンスがある場合はこれを使用してみてください

$('#<%=totalAmountLabel.ClientID%>')
于 2012-06-12T14:00:54.600 に答える
1

これを試して、ラベルのクライアント ID を取得してください。

var amount = document.getElementById("<%=totalAmountLabel.ClientID%>").innerHTML;
于 2012-06-12T13:56:56.197 に答える
0

<asp:Label>HTML<label>タグを生成していると思います。.valueその場合、属性はありません。.innerHTML代わりに試してください。

var amount = document.getElementById("totalAmountLabel").value;

する必要があります

var amount = document.getElementById("totalAmountLabel").innerHTML;
于 2012-06-12T13:57:18.363 に答える
0

JavaScript を次のように変更します (これを機能させるには、JS ファイルではなく、JavaScript コードをページで定義する必要があります<%= %>)。

 document.getElementById("<%= totalAmountLabel.ClientID %>").innerHTML = request.responseText;

または、javascript をそのままにして、ラベルを (4.0 の場合) に変更します。

<asp:Label ID="totalAmountLabel" runat="server" Text="Label" ClientIDMode="static"></asp:Label>

静的のクライアント ID モードにより、クライアントで ID が実際に totalAmountLabel になることが保証されます

于 2012-06-12T13:57:23.400 に答える