我尝试显示一个alert,如果input (inp_val)的值不在minvalue和maxvalue之间,但它不起作用,即使值在minvalue和maxvalue之间,它也会显示警报。
function compare(e){
this_id = e.id.slice(10,15);
inp_val = e.value;
minvalue = document.getElementById('min_' + this_id).value;
maxvalue = document.getElementById('max_' + this_id).value;
console.log(maxvalue);
console.log(minvalue);
console.log(inp_val);
if(inp_val < minvalue){
alert('Minimum Value: ' + minvalue + 'Input Value: ' + inp_val);
}
if(inp_val > maxvalue){
alert('Maximum Value: ' + maxvalue + ' Input Value: ' + inp_val);
}
}我调用函数compare与onBlur()进行比较
echo '<input class="result_fields" autocomplete="off" onBlur="compare(this)" id="result_inp' . $idc . '" type="text" name="test_res' . $idc . '" />'我曾尝试在jQuery中执行此操作,但出现了相同的错误。maxvalue和maxvalue是input类型的hidden,它的值存储在MySQL上的DB中,并使用php检索,如下所示:
echo '<input type="hidden" value="' . $val_test . '" name="test_name' . $idc . '" id="test_name' . $idc . '" class="testN" />';$idc++变量$idc是一个增量函数php。
发布于 2013-04-06 04:25:09
您需要根据从文本框中获得的值来解析整数或浮点数
如果它们是整数:
minvalue = document.getElementById('min_' + this_id).value;
maxvalue = document.getElementById('max_' + this_id).value;
minvalue = parseInt(minvalue,10);
maxvalue = parseInt(maxvalue,10);或者,如果它们是浮点数:
minvalue = document.getElementById('min_' + this_id).value;
maxvalue = document.getElementById('max_' + this_id).value;
minvalue = parseFloat(minvalue);
maxvalue = parseFloat(maxvalue);发布于 2013-04-06 04:23:51
您需要对整数值应用<和>运算符,以便将字符串值解析为整数,然后进行比较。
if(parseInt(inp_val,10) < parseInt(minvalue,10)){
alert('Minimum Value: ' + minvalue + 'Input Value: ' + inp_val);
}发布于 2013-04-06 04:24:09
试试这个:
if(!(inp_val >= minvalue && inp_val <= maxvalue)){
alert('Max Val: ' + maxvalue + ' Min Val: ' + minvalue + ' Input Value: ' + inp_val);https://stackoverflow.com/questions/15842797
复制相似问题