单击div时输入值未更改

时间:2017-12-04 20:33:03

标签: javascript jquery html

我想更改输入的值并在点击后提交。 由于某种原因,它没有被提交。

单击div时输入内的值会增加或减少,但它不会提交。

当我更改输入和离焦内的值时,它会保存。

我在这里缺少什么?

<div class="reduced items item-<?php echo $this->getQty() ?>" onclick="
var result_<?php echo $item->getId() ?> = document.getElementById('qty_<?php echo $item->getId() ?>'); 
var qty_<?php echo $item->getId() ?> = result_<?php echo $item->getId() ?>.value; 
if( !isNaN( qty_<?php echo $item->getId() ?> ) &amp;&amp; qty_<?php echo $item->getId() ?> > 1 ) result_<?php echo $item->getId() ?>.value--;
$(location.href='save').submit();
return false;">-</div>

<input type="text" name="quote_request[<?php echo $item->getId() ?>][qty][]" id="qty_<?php echo $item->getId() ?>" value="<?php echo $item->getQty()*1; ?>" size="4" title="<?php echo $this->__('Qty') ?>" onchange="location.href='save'" class="required-entry validate-zero-or-greater input-text" maxlength="12" />

<div class="increase items" onclick="
var result_<?php echo $item->getId() ?> = document.getElementById('qty_<?php echo $item->getId() ?>'); 
var qty_<?php echo $item->getId() ?> = result_<?php echo $item->getId() ?>.value; 
if( !isNaN( qty_<?php echo $item->getId() ?> )) result_<?php echo $item->getId() ?>.value++;
$(location.href='save').submit();
return false;">+</div>

1 个答案:

答案 0 :(得分:0)

那么, 首先,你不应该像你那样混合你的php / html / javascript代码。

其次,避免在变量名中放入id或其他任何代码值,而是使用具有等于id的键值的数组。

第三,使用函数,轻松查看正在发生的问题的方法是清理代码。

您的问题是因为您在jquery seletor中使用了location.href,我不知道您的表单是什么,但提交表单的正确方法是这样的:

function click_function_down(item_id) {
    var result[item_id] = document.getElementById('qty_' + item_id); 
    var qty[item_id] = result[item_id].value;
    if( !isNaN( qty[item_id] )
        && qty[item_id] > 1 ) {
            result[item_id].value--;
        }

    $('#yourform').submit();
    return false;
}

function click_function_up(item_id) {
    var result[item_id] = document.getElementById('qty_' + item_id); 
    var qty[item_id] = result[item_id].value;
    if( !isNaN( qty[item_id] ) ) {
            result[item_id].value--;
        }

    $('#yourform').submit();
    return false;
}

和html代码:

<form id="yourform" action="save">
<div 
class="reduced items item-<?php echo $this->getQty() ?>" 
onclick="click_function_up(<?php echo $item->getId() ?>)">
-
</div>

<input 
type="text" 
name="quote_request[<?php echo $item->getId() ?>][qty][]" 
id="qty_<?php echo $item->getId() ?>" value="<?php echo $item->getQty()*1; ?>" 
size="4" 
title="<?php echo $this->__('Qty') ?>" 
onchange="location.href='save'" 
class="required-entry validate-zero-or-greater input-text"
 maxlength="12" />

<div 
class="reduced items item-<?php echo $this->getQty() ?>" 
onclick="click_function_up(<?php echo $item->getId() ?>)">
-
</div>

试试这个,如果你能解决问题,请告诉我。