开发者

jQuery not updating span

开发者 https://www.devze.com 2023-02-08 10:11 出处:网络
I\'m pulling my hair out here using jQuery to update a span contents, but it\'s just not working! http://jsfiddle.net/PottyMonster/34mGm/

I'm pulling my hair out here using jQuery to update a span contents, but it's just not working!

http://jsfiddle.net/PottyMonster/34mGm/

I've included everything below, ev开发者_JAVA技巧en though i'm sure it's only this that isn't working:

$("#YouGet").html(StringToGoIn);

--- Html ---

&pound;<input onChange="CutCalculator();" id="CostField" type="text" class="ItemFieldCost" name="Cost" value="100" />
<span id=\"YouGet\">&pound;50.00</span>

--Script in Header ---

    function IsNumeric(strString)
//  check for valid numeric strings    
{
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    if (strString.length === 0) {
        return false;
    }

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult === true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}



function CutCalculator() {

    var Value = $('#CostField').val();

    if (IsNumeric(Value) === false) {
        alert("You didn't enter a valid value");
    }
    else {

        Value = parseFloat(Value);

        var Split = 0.5;
        var SupplierGets = Value * Split;
        var StringToGoIn = "&pound;" + SupplierGets;

        // alert(StringToGoIn );   <--- works fine
        // ---- WHY IS THIS NOT WORKING?!?! - URGH!!!
        $("#YouGet").html(StringToGoIn);

    }

}

Also, just out of interest, I've recently seen many people doing jQuery selectors like...

$('span[id=YouGet]').html(data);

Instead of

$("span#YouGet").html(StringToGoIn);

is there any reason for this?


You're escaping the quote characters in your ID name. Thus, the id of your span is "YouGet", not YouGet. Remove the escaping from the quotes in the HTML.


Take away the escapes id="YouGet" works


http://jsfiddle.net/mzm6g/

This seems to work.

0

精彩评论

暂无评论...
验证码 换一张
取 消