和字符串的数字onclick


sum string with number onclick

我有一个全局变量:

var a;

我把onclick函数放在我的htmldiv中,连接PHP:

$number =' <div onclick="select(this,'."'250000'".');" > ';
$number .= ...etc...  //php function
$number .='</div>';

要将字符串插入div值onclick(按钮):

function select (price){
if ($(id).attr('class') == "table2") { //unselect button
    $(id).attr('class', 'table1');
    var price2=document.getElementById("price").value.split(",");
    var removePrice = price;;
    var price3 = jQuery.grep(price2, function (value) { return value != removePrice;});
    document.getElementById("price").value = price3;
    if (n.length == 0) i = 0;
} else {
    $(id).attr('class', 'table2'); //select button
    if (i == 0) {
        document.getElementById("price").value = price;
        a = Number(document.getElementById("price").value); //assign the value into a
        i = 1;  
    } else {
        $(id).attr('class','table3');
    }  
}

从那里,我有复选框HTML:

<div id="CourseMenu"><input type="checkbox" value="50000" />&nbsp Ekstra Bed <input type="checkbox" value="50000"  />&nbsp Breakfast</div>

之后,我有另一个函数来求和2个div值(复选框):

$(function($) {
  var sum = 0;
  $('#CourseMenu :checkbox').click(function() { sum = 0;
    $('#CourseMenu :checkbox:checked').each(function(idx, elm) {
        sum += parseInt(elm.value, 10);
    });
    var b  = parseInt(a||0, 10) + parseInt(sum||0, 10); 
    $('#sum').html(b); //<--resulted NaN when sum with button
    document.getElementById("price").value=b; //<-- assign b as div value
  });
});

目的是将总价(按钮和复选框)相加为div值

<input type="hidden" id="price" name="price" value="" />

当函数只对复选框进行求和时,它运行良好,但当我尝试用按钮(全局变量)显示求和时,它会导致NaN

我想我已经把字符串转换成数字了,我的代码有问题吗?

我认为您在传递onclick事件中的参数时遇到了问题:onclick="select(this,'250000');"=>将this作为第一个参数传递,尝试更改为onclick="select('250000`)";

但是您的select函数期望字符串作为第一个参数:

function select(price){ //<-- simplified
   document.getElementById("price").value=price;
   a = Number(document.getElementById("price").value); 
}

以下是X/Y问题的实际解决方案。

代码将初始化字段,单击按钮或复选框可以独立工作。

function calc() {
  var sum = parseInt($("#price").text(), 10);
  $('#CourseMenu :checkbox:checked').each(function() {
    sum += parseInt(this.value, 10);
  });
  $('#sum').val(sum);
}
$(function() {
  $("#CourseMenu .btn").on("click", function() {
    $("#price").text($(this).data("price")); // using the data attribute
    calc(); 
  });
  $('#CourseMenu input:checkbox').on("click",function() { 
    calc(); // 
  });
  calc(); // initialise in case the page is reloaded.
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="CourseMenu">
  <button type="button" class="btn" data-price="25000">25000</button>
  <button type="button" class="btn" data-price="35000">35000</button>
  <div id="price">0</div>
  <br/>
  <label>
    <input type="checkbox" value="200" />200</label>
  <label>
    <input type="checkbox" value="300" />300</label>
  <label>
    <input type="checkbox" value="400" />400</label>
  <br/>
  <input type="text" id="sum" value="" />
</form>

PS:如果你点击复选框不做任何其他事情,你可以写

  $('#CourseMenu input:checkbox').on("click",calc);