【问题标题】:jquery - how to add two variables from .html()jquery - 如何从 .html() 添加两个变量
【发布时间】:2013-05-12 08:59:39
【问题描述】:

我是新手,很难理解。如何使用 .html() 添加两个变量。我得到的只是 Nan(不是数字)或 [object Object]。我已经尝试过 parseInt 和 +。

$("select#description").change(function()
{
    $("select#page").html("<option>wait...</option>");
    var id2 = $("select#description option:selected").attr('value');
    $.post("./includes/select_insidepage.php", {id2:id2}, 
    function(data){
    $("select#page").removeAttr("disabled");
    $("select#page").html(data);

            //show price---------------------------
            $("#price").html(data);

           //start----------------------orientation
            $("select#page").change(function()
            {
            $("select#orientation").html("<option>wait...</option>");
            var id3 = $("select#page option:selected").attr('value');
            $.post("./includes/select_orient.php", {id3:id3},
            function(data1){
            $("select#orientation").removeAttr("disabled");
            $("select#orientation").html(data1);

        //show insidepagePrice----------
        $("label#insidepage").html(data1);

        //computation------------------                     
        var pagePrice = $("#insidepage2").html(data1);
        var prodPrice = $("#price2").html(data);                
        $("#totalprice").html(+pagePrice + +prodPrice);

将从 jquery 显示的html

<tbody>
                <tr class="top row">
                    <td align="left"> <select id="description"></select>
                </td>
                    <td align="center"> <label id="price"> </label></td>
                </tr>
                <tr class="side">
                    <td align="left"><select id="page"></select>
                </td>
                    <td align="center"> <label id="insidepage"> </label></td>
                </tr>

                <tfoot>
                <tr>
                    <td colspan="3">&nbsp;  </td>
                    <td class="leftfoot" align="left"> <b> subtotal </b></td>
                    <td class="rightfoot" align="center"><label id="totalprice"> </label> </td>

将显示在下拉列表中的查询

public function ShowType()
        {
            $sql = "SELECT * FROM description WHERE id_cat = $_POST[id]";
            $res = mysql_query($sql,$this->conn);
            $type = '<option value="0">choose...</option>';         
            while($row = mysql_fetch_array($res))
            {
                $type .= '<option value="' . $row['id_type'] . '">' . $row['descrip'] . '</option>';
            }
            return $type;
        }

                    public function productPrice()
                    {
                        $sql = "SELECT prodPrice FROM description WHERE id_type = $_POST[id2]";
                        $res = mysql_query($sql,$this->conn);
                        $row = mysql_fetch_array($res);
                        $price = '<label>' . $row['prodPrice']  . '</label>';
                        return $price;
                    }

        public function ShowPage()
        {
            $sql = "SELECT * FROM insidepage WHERE id_p = $_POST[id2]";
            $res = mysql_query($sql,$this->conn);
            $page = '<option value="0">choose...</option>';
            while($row = mysql_fetch_array($res))
            {
                $page .= '<option value="' . $row['id_price'] . '">' . $row['insidepage'] . '</option>';
            }
            return $page;
        }

                    public function insidepagePrice()
                    {
                        $sql = "SELECT pagePrice FROM insidepage WHERE id_price = $_POST[id3]";
                        $res = mysql_query($sql,$this->conn);
                        $row = mysql_fetch_array($res);
                        $inside = '<label>' . $row['pagePrice'] . '</label>';
                        return $inside;
                    }

【问题讨论】:

  • datadata1 是什么? $("#insidepage2").html(data1) 返回一个 jquery 对象,这就是你得到 o/p 的原因
  • 试试`$("#totalprice").html(+data1 + +data)`
  • @ArunPJohny 讨厌.......:-)
  • var pagePrice = $("#insidepage2").html(data1); 这会在“pagePrice”中返回一个 JQuery 对象。 .html(Object1 + Object2);我确定这不是你想做的^^。您的数据类型是什么?只有字符串?
  • @Arun P Johnydata 和 data1 只是数字。 $("#totalprice").html(+data1 + +data) 也返回 NaN

标签: jquery mysql nan


【解决方案1】:

试试这个。我删除了你的几行来展示(例如)如何添加数据。 你必须使用 $.each(data, function(key, value) { a = value; }); 将 [object Object] 转换为值。

我不确定它不能解决你的全部问题,因为我不知道你会在value得到什么

更新

$(document).ready(function() {
    GetValue();
});

function GetValue() {

    $("#description").change(function() {
        $("#page").empty();
        $("#page").html("<option>wait...</option>");
        $.post("./includes/select_insidepage.php",
        {
            id2: $("#description option:selected").text()
        },
        function(data) {
            $("#page").empty();
            $.each(data, function(key, value) {
                alert('alert 1 : ' + value);
                $('#page').append('<option>' + value + '</option>');
            });
            $("#price").html($('#page option:selected').text());
            var Page = $('#page option:selected').text().split(' Page')[0];
            var Orientation = $('#orientation option:selected').text();

            $("#totalprice").html(parseFloat(Page) + parseFloat(Orientation));
        }, 'json');
    });

    $("#page").change(function() {
        $("#orientation").empty();
        $("#orientation").html("<option>wait...</option>");
        $.post("./includes/select_orient.php",
        {
            id3: $("#page option:selected").text()
        },
        function(data) {
            $("#orientation").empty();
            $.each(data, function(key, value) {
                alert('alert 2 : ' + value);
                $('#orientation').append('<option>' + value + '</option>');
            });
            $("#insidepage").html($('#orientation option:selected').text());

            var Page = $('#page option:selected').text().split(' Page')[0];
            var Orientation = $('#orientation option:selected').text();

            $("#totalprice").html(parseFloat(Page) + parseFloat(Orientation));
        }, 'json');
    });

}

【讨论】:

  • @Celso Tengonciang 复制我的代码并说出你在 alert 1alert 2 中得到了什么。我不知道data 包含什么。所以在不知道的情况下,我无法帮助你。
  • 非常感谢您的帮助,谢谢。我从 alert1 得到这个结果,单行 警告:2 mysql_fetch_a2ray() 期望 p process.js:30 a process.js:30 r process.js:30 一个进程。 js:30 m process.js:30 e process.js:30 t process.js:30 e process.js:30 r process.js:30 process.js:30
  • @Celso Tengonciang 我不知道它是什么样的错误。在这两个警报中,您应该一个一个地获取您的列表框项目。
  • 是的,我在使用你的 $.each(data, function(key, value) 之前得到了这个。但我的目标是在他们从下拉列表中选择时将价格作为数字。谢谢
  • 其实我不懂php。尝试不同的东西。在stackoverflow中,我可以建议或帮助你。我不能做你的完整编码。
猜你喜欢
  • 2011-04-18
  • 2014-05-30
  • 1970-01-01
  • 2021-03-31
  • 2021-08-22
  • 2022-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多