検索
ホームページウェブフロントエンドjsチュートリアルjquery 検証での単一選択と複数選択の例 Forms_jquery

たとえば、次のオプションのうち、* の付いたオプションを選択する必要があります。図を参照してください:

jquery 検証での単一選択と複数選択の例 Forms_jquery

上のページをご覧になりましたか?ビジネス処理チャネルで複数の選択肢の中から 1 つを選択する必要があります。どうすればよいですか?何も選択されていない場合は、フォームの送信時にエラー メッセージが表示されます。 jQuery の入力検証は非常に簡単であることは誰もが知っていますが、複数の選択または単一の選択を検証する必要があることはほとんどありません。さらに、これはページ上のすべてではなくグループに分割されるグループ検証です。 !何をするか?

jquery の代わりに他のものを使用しても問題ありませんか?できる! ! !ただし、実装には依然として jquery を使用します。

最初に、選択されていない効果は次のようになります:

jquery 検証での単一選択と複数選択の例 Forms_jquery

いずれか 1 つが選択されていると、以下に示すようにプロンプ​​ト メッセージがすぐに消えます:

jquery 検証での単一選択と複数選択の例 Forms_jquery

実際、これは jsmap を使用して実現できます。つまり、js を使用してマップをシミュレートします。次のコードは公開されています。今後この状況が発生した場合は、次の js コードをコピーして、いくつかの設定を行ってください。

次のコードを使用します。jquery 検証 js を導入する必要があります

コードは次のとおりです:

まず、次の js コードを js ファイルまたはページに追加します:

コードをコピーします コードは次のとおりです:

/**
* 数组存储器 主要是为了方便juery验证chckbox而设计                   数组存储器,配置对象,为验证checkbox 和单选。
* 私有变量区定义数组
* 然后把每一个数组配置到setting里面,这样这个数组就存在于存储器中
* 在应用的时候用数组名即可方便地操作和得到不同的数组。
* 当你需要某个数组的时候,如果你只操作一个数组那这个方法是多于的,但如果你操作多个数组或只有数组名的情况下,这个存储器就很有用。
* @author 电子科大科园 庄濮向
* @return 数组存储器对象
*/
var MapArr = (function () {

    var BHC = [], BCC = [], BQC = [], IC = [];
    var BAC = [];
    var BUC = [];

    var setting = [
            {
                key: "BHC",
                value: BHC
            },
            {
                key: "BAC",
                value: BAC

            }, { key: "BUC", value: BUC }, { key: "BCC", value: BCC }, { key: "BQC", value: BQC }, {key:"IC",value:IC}
            ];

    return {

        //通过数组名得到数组织
        getArr: function (arr_name) {
            for (var setting_i = 0, setting_len = setting.length; setting_i                 if (setting[setting_i].key == arr_name) {
                    return setting[setting_i].value;
                }
            }
        },
        //删除指定数组中的某一个元素
        delArr: function (arr_name, elementValue) {
            for (var delArr_i = 0, delArr_len = setting.length; delArr_i                 if (setting[delArr_i].key == arr_name) {
                    for (var arr_i = 0, arr_len = setting[delArr_i].value.length; arr_i                         if (setting[delArr_i].value[arr_i] == elementValue) {
                            setting[delArr_i].value.splice(arr_i, 1);
                        }
                    }

                                                                                                                                                   for (var pushArr_i = 0, pushArr_len = setting.length; pushArr_i                                                                                                                                          }
}
}
}

} ());


/**
*The click event of the check box or radio is applied to the above map
* @author Zhuang Puxiang, University of Electronic Science and Technology of China

* @return array memory object

*/

function chk(event, arrName) {


if (event.checked == true) {
//Add an element to the array named arrName
MapArr.pushArr(arrName, event.value);
} else {
            //Remove an element from the array named arrName
       MapArr.delArr(arrName, event.value);

   }


var checkInput = document.getElementById(arrName);

if (MapArr.getArr(arrName)[0] == 'undefined' || MapArr.getArr(arrName)[0] == null) {
                                                                                                   checkInput.value = null; //Give him the value of this array. In this case, it has the effect of verification
} else {
} checkInput.value = MapArr.getArr(arrName)[0];

}

//How to make the prompt information appear after adding the value Disappear?
if (!$("#form1").valid()) return false; //Just to make this chapter expand and disappear, so don't run the submitted check, it will affect the display of the chapter
}


Two: Add an input after the group of titles of the multi-select or single-select you need to verify:




Copy Code



The code is as follows:
3: Add events to your checkbox or radio tag:


Copy code

The code is as follows:

onclick="chk(this,'BAC')" Four: Consistency: The input in "two" is used for prompt information, so your id and name must be there, and the names must be consistent, as above: id= "BAC", name="BAC", after the input is written, when adding the event, your second parameter must have the same name as the id of the input, and it must also be BAC;
Five: Configuration, once the face is ready, it is the core configuration. Where to configure this configuration, it is in the js code I asked you to introduce above. onclick="chk(this,'BAC')" is to add an event to each check or radio selection. It also means that this check should be applied to the BAC array, so we go to the setting Configure an array.

Add an array to MapArr:

Copy code


The code is as follows:

var BAC =[];Add an object to the setting of MapArr:


Copy the code


The code is as follows :
Be sure to ensure consistency, otherwise it will not succeed .
Key points: The id of input is consistent with the second parameter of onclick. The variable name of the array variable added in MapArr is consistent with the id of input. The value of key in setting is consistent with the id of input. The value of value in setting is you. This array is configured. Okay, now you can implement verification.
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
jquery实现多少秒后隐藏图片jquery实现多少秒后隐藏图片Apr 20, 2022 pm 05:33 PM

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

jquery怎么修改min-height样式jquery怎么修改min-height样式Apr 20, 2022 pm 12:19 PM

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

axios与jquery的区别是什么axios与jquery的区别是什么Apr 20, 2022 pm 06:18 PM

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

jquery怎么在body中增加元素jquery怎么在body中增加元素Apr 22, 2022 am 11:13 AM

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

jquery中apply()方法怎么用jquery中apply()方法怎么用Apr 24, 2022 pm 05:35 PM

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

jquery怎么删除div内所有子元素jquery怎么删除div内所有子元素Apr 21, 2022 pm 07:08 PM

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

jquery怎么去掉只读属性jquery怎么去掉只读属性Apr 20, 2022 pm 07:55 PM

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

jquery on()有几个参数jquery on()有几个参数Apr 21, 2022 am 11:29 AM

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター