简体   繁体   English

保存输入值onclick div

[英]Save input value onclick div

Next to a input I want to display two div's with plus and minus signs, so that when clicking on one of the divs, the value insdie the input increase or decrease. 在输入旁边,我想显示带有加号和减号的两个div,以便在单击一个div时,该值表示输入的增加或减少。

It almost works perfect, but the best way to save the input. 它几乎可以完美工作,但却是保存输入的最佳方法。 Is by changing the input and than load the /save url. 是通过更改输入,然后加载/save url。 The input works perfect with this code, that on changing the /save url loads and the value is saved. 输入与此代码完美配合,即更改/ save url加载并保存该值。

I want the same for the div's, that when clicking on the div the value inside the input changes and the value is saved by the /save URL. 我希望div保持不变,即在div上单击时,输入中的值会更改,并且该值由/save URL /save

How do I need to change my code for this? 我该如何更改我的代码?

CODE HTML: 代码HTML:

            <div class="quote-item product-quote-qty">
                    <div id="qdiv_<?php echo $item->getId() ?>" nowrap="nowrap" class="qty-div input-box">
                    <div class="reduced items item-<?php echo $item->getQty()*1; ?>" 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--;saveForm();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++;saveForm();return false;">+</div>
                    </div>
            </div>

CODE JS: 代码JS:

    function saveForm() {
        var form = $('quotelist').clone(true);
        //update textarea
        var i = 0;
        $('quotelist').select('textarea').each(function (el) {
            form.select('textarea')[i].value = $(el).value;
            i++;
        });

        var action = $('quotelist').action;
        action = action.replace("quoteRequest", "save");
        form.action = action;
        form.request({
            onComplete: function(){ Miniquote.reloadContent(); }
        });
    }

    function addQtyFieldSaver(){
        $$('#shopping-cart-table input[type=text]').each(function (el) {
            return $(el).observe('blur', function(e){
                saveForm();
            });
        });
    }

EDIT: 编辑:

    function addQtyFieldSaver(){
        $$('#shopping-cart-table input[type=text]').each(function (el) {
            return $(el).observe('blur', function(e){
                saveForm();
            });
        });

    $('.reduced').click(function () {
    var el =  $(this).parent().find('input:text');      
    var newval = (parseInt($(el).val(),10) - 1 > 0)?$(el).val() - 1:0;
    el.val(newval);
    saveForm();
    });

    $('.increase').click(function () {      
    var el = $(this).parent().find('input:text');
    var newval = parseInt($(el).val(),10)+1;
    el.val(newval);
    saveForm();
    });

    }

Get rid of your PHP code in your plus and minus divs like this 像这样摆脱正负div中的PHP代码

<div class="reduced items">-</div>
<div class="increase items" >+</div>

And add this to your addQtyFieldSaver function 并将其添加到您的addQtyFieldSaver函数

    $('.reduced').click(function () {
        var el =  $(this).parent().find('input:text');      
        var newval = (parseInt($(el).val(),10) - 1 > 0)?$(el).val() - 1:0;
        el.val(newval);
        saveForm();
    });
    $('.increase').click(function () {      
        var el = $(this).parent().find('input:text');
        var newval = parseInt($(el).val(),10)+1;
        el.val(newval);
        saveForm();
    });

With this solution, the click event on the +/- divs accesses the input text field using its parent container in order to get the quantity of the input value and change it. 使用此解决方案,+ /-div上的click事件使用其父容器访问输入文本字段,以获取输入值的数量并对其进行更改。 Then calls the function saveForm() to save the new data. 然后调用函数saveForm()保存新数据。

EDIT: Full Example 编辑:完整示例

 function saveForm() { alert('saveform is called'); } function addQtyFieldSaver(){ $('#shopping-cart-table input[type=text]').each(function (el) { return $(el).bind('blur', function(e){ saveForm(); }); }); } $(document).ready(function(){ addQtyFieldSaver(); $('.reduced').click(function () { var el = $(this).parent().find('input:text'); var newval = (parseInt($(el).val(),10) - 1 > 0)?$(el).val() - 1:0; el.val(newval); saveForm(); }); $('.increase').click(function () { var el = $(this).parent().find('input:text'); var newval = parseInt($(el).val(),10)+1; el.val(newval); saveForm(); }); }) 
 <html> <head> <title>Ejemplo</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script type="text/javascript"> </script> </head> <body> <div id="shopping-cart-table"> <div class="quote-item product-quote-qty"> <div id="qdiv_1" nowrap="nowrap" class="qty-div input-box"> <div class="reduced items">-</div> <input type="text" name="quote_request[1][qty][]" id="qty_1" value="1" size="4" title="1" onchange="location.href='save'" class="required-entry validate-zero-or-greater input-text" maxlength="12" /> <div class="increase items" >+</div> </div> </div> </div> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM