使用Java相同的操作将带有输入和提交按钮的表单相乘 [英] Multipile forms with input and submit button with the same action with Javascript

查看:97
本文介绍了使用Java相同的操作将带有输入和提交按钮的表单相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带输入的表单和一个提交按钮(JavaScript), 我创建了3个表单,以创建3个不同的选项,尽管这些选项针对输入错误和提交使用相同的Javascript, 如果我在尝试使用第二个或第三个表单时没有在页面的第一个表单中输入值,这会给我一个错误.

I have a Form with Input and a submit button (Javascript), I duplicated the form 3 times in order to create 3 different options, although these options use the same Javascript for input errors and for submission, this resulted in giving me an error if i do not enter value on the first form on the page, while I am trying to use the second or third form.

JS文件:

jQuery(function($){
$('form#unlock').on('submit', function (e){
    if($('#the_imei').val().length == 15){
        if($('#the_imei').val().indexOf('9900') === 0){
         alert('לפי המספר IMEI, ברשותכם מכשיר CDMA, אנא ראו מידע נוסף בעמוד פתיחת מכשירי CDMA');
         window.location = 'http://www.unlocker.co.il/sim-unlock-cdma-mobile-device';
         e.preventDefault();
             }
        return;
    }

    alert('אנא מלאו מספר IMEI בעל 15 ספרות');
    e.preventDefault();
}); 
})

页面URL为: http://www .unlocker.co.il/shop/sim-unlock-htc-mobile-device/

感谢您的帮助

推荐答案

首先想到的是,如果您只是通过复制并粘贴表单代码来复制表单,则是在复制任何id. ID必须是唯一的.如果您有多个同名的ID,则Javascript只会识别页面中的第一个ID.

The first thing that came to mind is if you are simply duplicating the form by copy and pasting the form code, is that you are duplicating any id's. ID's need to be unique. If you have more than one id with the same name, then Javascript will only recognize the first one in the page.

为您的第一个表格更具体...(请注意将id更改为class的输入)

To be more specific for your first form... (notice the input change of id to class)

<form id="unlock1" class="cart" enctype="multipart/form-data" method="post" name="unlock">
        <input class="the_imei" style="width: 80%; border-radius: 15px;" name="the_imei" type="text" value="" placeholder="מספר סידורי IMEI של המכשיר (חייג #06#*)"><br>
        <input class="add-to-cart" name="add-to-cart" type="hidden" value="39"><br>
        <button class="unlockButton" type="submit" value="submit">פתח לכל הרשתות בישראל </button>
</form>

然后输入您的脚本...

Then for your script...

    jQuery(function($){
    $('form#unlock1').on('submit', function (e){
        if($('form#unlock1 > input.the_imei').val().length == 15){
            if($(this).val().indexOf('9900') === 0){
             alert('לפי המספר IMEI, ברשותכם מכשיר CDMA, אנא ראו מידע נוסף בעמוד פתיחת מכשירי CDMA');
             window.location = 'http://www.unlocker.co.il/sim-unlock-cdma-mobile-device';
             e.preventDefault();
                 }
            return;
        }

    alert('אנא מלאו מספר IMEI בעל 15 ספרות');
    e.preventDefault();
    }); 
   })

这篇关于使用Java相同的操作将带有输入和提交按钮的表单相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆