开发者

Using a variable in jQuery to perform a function

Can someone tell me what's wrong with this code? It doesn't seem to be recognizing the '#del[' + cid + ']' selector, at all. Which is the exact name of the ID in my PHP code.

$(function() {
    var cid = '<?=$row['c_id'];?>';
    $('#del[' + cid + ']').click(function() {
        alert('clicked!');
        var oldqty = <?=$row['qty'];?>;
        var qtyID = "'" + '#qty' + cid + "'";
        alert(qtyID);
        if ($(qtyID).is(':checked')) {
            $(this).(function() {
                $(this).val(0);
            });
        };
        if($(qtyID).not(':checked')) {
            $(this).(function() {
                $(this).val(0);
            });
        };
    });
});

Here's the PHP code that implement $row['c_id']:

echo "<input class=\"number aln_center\" type=\"text\" name=\"qty[" . $row['c_id'] . "]\" id=\"qty" . $row['c_id'] . "\" value=\"" . $row['qty'] . "\" size=\"3\" oncha开发者_如何学JAVAnge=\"return validateChgMLQty('qty" . $row['c_id'] . "'," . $row['qty'] . ");\" />\n";
echo "<input type=\"hidden\" name=\"telco[" , $row['c_id'] . "]\" id=\"telco" . $row['c_id'] . "\" value=\"" . $row['btelco'] . "\" />\n";
echo "<br />Delete\n";
echo "<input type=\"checkbox\" name=\"del[" . $row['c_id'] . "]\" id=\"del" . $row['c_id'] . "\" />\n";

I'm trying to get the value in the input statement to change to "0" if the "Delete" checkbox is clicked and then return back to it's original contents when unchecked. It never even gets to the first alert box, so it has nothing to do with qtyID and when viewing source, the 'var cid' line is populated with the correct integer passed from PHP variable $row['c_id'].


var qtyID = "'" + '#qty' + cid + "'";

This line is a problem. You shouldn't be adding a quote to the beginning and end of the string.

This will make a string like: "'#qty6'", which will not work in a jQuery selector.

It should just be:

var qtyID = '#qty'+cid;

This selector:

 $('#del[' + cid + ']')

Should be:

 $('#del'+cid)


Thanks to all!

The final solution was:

$(function() {
    var cid = "<?=$row['c_id'];?>";
    var oldqty = "<?=$row['qty'];?>";
    $('#del' + cid).change(function() {
        if ($('#del' + cid).is(':checked')) {
            $('#qty' + cid).val(0);
            return;
        };
        if($('#del' + cid).not(':checked')) {
            $('#qty' + cid).val(oldqty);
            return;
        };
    });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜