本文介绍了jquery提交表单之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表(一个简单列表),我可以从中选择和设置元素(使用js),然后是一个允许我选择多少元素以及提交表单的表单。没有选择一个元素,有一个引发异常的脚本。问题是,我希望表单不提交,如果没有选择一个元素,但不抛出一个异常,但向我展示一个消息提交按钮(使用jQuery)。我的脚本如下:

 <? foreach($ types为$ type):?> 
< ul class =product_types>
< li id ='product_types'>< a href =#onclick ='selecteazaElement(<?= $ type-> id;?>,<?= $ type-> ; stock_2;?>);'><?= $ type-> label; ?>< / A>< /锂>
< li id ='product_unavailable_types'>< label><?= $ type-> label; ?>< /标签>< /锂>

< / ul>
< form name =addtobasketmethod =POSTaction =<?= Route :: url('Add to Basket',array('sale_id'=> $ sale-> id) );?>>
< input type =hiddenname =idOfSelectedItemid =idOfSelectedItemvalue = - 1>
< select name =numberid =number>
< option value = 0> Alege numarul de produse< / option> < /选择>
< button type =submitname =submitonclick =addtobasket;> Adauga in cos< / button>< br />
< / form>

以及设置列表元素的js:

 < script type =text / javascript> 
函数selecteazaElement(id,stock)
{
document.addtobasket.idOfSelectedItem.value = id;
window [canSubmit] = true;
var number23 = document.addtobasket.number;
number23.options.length = 0;
if(stock> = 6)
stock = 6;
for(i = 1; i {
// alert('id:'+ id +'; stock:'+ stock);
number23.options [number23.options.length] = new Option(i,i);
}
//window.status=\"my status;


解决方案

添加向表单提交侦听器。提交时,请检查是否选择了元素,如果不是,则可以使用 return false 来阻止提交。这里有一个例子:

$ p $ $('#myForm')。submit(function()
{
if(/ * test case not true * /){
$('#myError')。show();
return false;
}

// ...继续工作
});

这里是HTML:

 < form id =myForm> 
< input type =text/>
< input type =submit/>
< / form>

如果您不想使用jQuery,您还可以使用普通的JavaScript来处理submit事件所以:

  var myform = document.getElementById('myForm'); 
myform.addEventListener('submit',function(){console.log('Submitted!'); return false;});


I have a list, (a simple list) from which i am able to select and set elements (using js), and then a form that allows me to choose how many elements i want, and a submit form.if one doesn't select an element, there is a script that throws an exception. The problem is that i want that the form doesn't submit, if an element is not selected, but not throw an exception, but to show me a message down the submit button (using jquery). my script below:

<? foreach ($types as $type):?>
<ul class = "product_types">
    <? if ($type->stock_2 > 0):?>
    <li id = 'product_types'><a href="#" onclick='selecteazaElement(<?= $type->id; ?>,<?= $type->stock_2; ?>);'><?= $type->label; ?></a></li>
    <? else: ?>
    <li id = 'product_unavailable_types'><label><?= $type->label; ?></label></li>
    <? endif; ?>

</ul>
<? endforeach; ?>
<form  name="addtobasket" method="POST" action="<?= Route::url('Add to Basket', array('sale_id' => $sale->id)); ?>">
    <input type="hidden" name="idOfSelectedItem" id="idOfSelectedItem" value="-1">
    <select name="number" id="number">
        <option value=0>Alege numarul de produse</option>    </select>
    <button type="submit" name = "submit" onclick="addtobasket";>Adauga in cos</button><br />
</form>

and the js that sets the list elements:

   <script type="text/javascript">
   function selecteazaElement(id,stock)
    {
   document.addtobasket.idOfSelectedItem.value=id;
   window["canSubmit"] = true;
   var number23=document.addtobasket.number;
   number23.options.length=0;
   if (stock>=6)
   stock=6;
   for (i=1;i<=stock;i++)
    {
//alert ('id: '+id+'; stock: '+stock);
   number23.options[number23.options.length]=new Option(i, i);
   }
//window.status="my status";
}
解决方案

Add a submit listener to the form. When it's submitted, check to see if an element is selected, and if not you can prevent the submission by using return false. Here's an example:

$('#myForm').submit(function()
{
    if (/* test case not true */) {
        $('#myError').show();
        return false;
    }

    // ... continue work
});

And here's the HTML:

<form id="myForm">
    <input type="text"/>
    <input type="submit"/>
</form>

If you don't want to use jQuery, you can also handle the submit event with plain JavaScript like so:

var myform = document.getElementById('myForm');
myform.addEventListener('submit', function() { console.log('Submitted!'); return false; });

这篇关于jquery提交表单之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:56
查看更多