English 中文(简体)
How to correct a jQuery syntax error?
原标题:

I m trying to update a Total: (span.wrap p span) field at the bottom of a form using jQuery.

I have a few checkboxes, each of which has an associated price contained within the title attribute like so: title="$2".

I d the Total: field to update dynamically when the user clicks a checkbox. Here is the script I have, but it s throwing a syntax error as shown.

$("input:checkbox").toggle(
        var v = $(this).attr("title").substr(-1) * 1; // syntax error here. subtstr() eliminates the $ sign
        var t = $("span.wrap p span").text() * 1; // converts the string to a number
        function () {
            $("span.wrap p span").text(t + v);
        },
        function () {
            $("span.wrap p span").text(t - v);
        }
    );

Note: The total field has an initial value of 0. There are four checkboxes; perhaps I need to use each()?

Any ideas on how to make this work?

最佳回答

Toggle accepts a function

$("input:checkbox").toggle(function() {
   var v...
问题回答

When you use a toggle function, afaik you can only define code within the two function blocks.

change:

var v = $(this).attr("title").substr(-1) * 1

to

var v = $(this).attr("title").substr(1) * 1




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

pdo database abstraction

Can someone help me to see what is going wrong with this setup I build the @sql query in the function below like this. The extra quotes are setup in the conditions array. $sql .= " WHERE $...

I wish I could correlate an "inline view"

I have a Patient table: PatientId Admitted --------- --------------- 1 d/m/yy hh:mm:ss 2 d/m/yy hh:mm:ss 3 d/m/yy hh:mm:ss I have a PatientMeasurement table (0 to ...

Syntax help! Php and MYSQL

Original: $sql = "SELECT DATE(TimeAdded) AS Date, $column_name FROM Codes ORDER BY TimeAdded ASC"; Altered: $sql = "SELECT DATE("m", TimeAdded ) AS Date, ColumnName FROM TableName ORDER BY ...

Is this code Equivalent

I am not a fan of the following construction if (self = [super init]) { //do something with self assuming it has been created } Is the following equivalent? self = [super init]; if (self != ...

热门标签