繁体   English   中英

从中执行Javascript函数

[英]Execute Javascript Function from From

很抱歉这个简单的问题,但是我在任何地方都找不到答案...我也是一个Java新手,有0年的经验,所以我真的很困惑。

无论如何,我有一个函数,只需单击一下按钮即可使用:

<input type="button" value="Add Item" onclick="addToCartt('MODELNUMBER');">

我需要从当前设置中更改该设置,在当前设置中,您可以单击并单击一个按钮,然后将带有ModelNumber参数的函数运行到一个表单中,在该表单中您手动输入ModelNumber并通过html表单命中return,并使用ModelNumber作为函数运行addToCartt函数论据。

谢谢你的帮助

function addToCartt(product_id, quantity) {
      quantity = typeof(quantity) != 'undefined' ? quantity : 1;

      $.ajax({
        url: 'index.php?route=checkout/cart/addorder',
        type: 'post',
        data: 'product_id=' + product_id + '&quantity=' + quantity,
        dataType: 'json',
        success: function(json) {
          $('.success, .warning, .attention, .information, .error').remove();



        $('#maincontent').addClass('active');
        $('#maincontent').load('index.php?route=module/cart');
        // $('#cart').live('mouseleave', function() {
        // $(this).removeClass('active');
        // });


          if (json['redirect']) {
            location = json['redirect'];
          }

          if (json['success']) {
            $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="close.png" alt="" class="close" /></div>');

            $('.success').fadeIn('slow');

            $('#cart-total').html(json['total']);
            $('#new_total').html(json['new_total']);

            // $('html, body').animate({ scrollTop: 0 }, 'slow'); 
          } 
        }
      });
    }

HTML:

<form name='frm'>
    <input type='text' id='model' />
</form>

javascript:

$(function() {
    $('#model').keypress(function(e){
        if (e.which === 13) {
            $(frm).submit()
        }
    });
    $('form').submit(function(e) {
        e.preventDefault();
        addToCart($('#model').val());
    });
});

如果模型是表单中唯一的输入,则不需要keypress事件。

这可能是您想要的HTML。 在看不到您的JavaScript的同时,我无能为力:

<form onsubmit="return addToCart();">
   <input type="text" value"MODELNUMBER" />
   <input type="submit" value="Add Item"/>
</form>

您需要从addToCart返回false,以防止表单提交新页面加载。 如果使用jQuery,还可以在event参数上调用preventDefault

您需要从html输入中获取值。 使用jQuery将是这样的:

<input type="button" value="Add Item" onclick="addToCart($('#modelNumber').val());">

暂无
暂无

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

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