。我有一個輸入,我通過選擇每個單選按鈕來改變名稱屬性和其他屬性。現在我通過jquery.validation檢查輸入的每個狀態的name屬性,并顯示其錯誤
我如何使用另一個屬性來代替name屬性?
我怎樣才能使用另一個屬性來做這件事,而不是使用name屬性呢?
$("input[type=radio]")。 prop("checked", false) 。
$("input[type=radio]:nth-child(1)").prop("checked", true)。
$("input[type=radio]").click(function (event) {
$("label.error"/span>).remove()。
$("input").removeClass('error') 。
$("#BillIdentity").val('')。
$("input[type=radio]").prop("checked", false) 。
$(this).prop("checked", true) 。
var id = $(this).attr("id") 。
console.log(id)。
if (id == "BillIdentitySelect") {
$("#BillIdentity").attr("maxLength"/span>, "30")
$("#BillIdentity"/span>).attr("name", "BillIdentityMode")
$("#BillIdentity").attr("placeholder", "插入賬單id")
//if ($('.iinput').value.length > 10) { return false; }.
}
else if (id == "NationalCodeSelect"/span>) {
$("#BillIdentity").attr("maxLength", "10" )
$("#BillIdentity").attr("name", "NationalCodeMode")
$("#BillIdentity").attr("placeholder", "inser national code")
}
//event.preventDefault(); })
});
var $form = $("form") 。
$form.validate({
rules: {
BillIdentityMode: {
required: true,
minlength: 11。
},
NationalCodeMode: {
required: true,
minlength: 10, minimum length: 10
}
},
messages: {
BillIdentityMode: "您必須輸入至少11個字符"。
NationalCodeMode: "該號碼是錯誤的!", NationalCodeMode: "該號碼是錯誤的!"
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>/span>
<script src="https://cdnjs.cloudflare. com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>/span>
<div class="radioGroup">/span>
< input type="radio" id="BillIdentitySelect" name="radio1" />
<label for="BillIdentitySelect"/span>> 賬單ID</label>
< input type="radio" id="NationalCodeSelect" name="radio2" />
<label for="NationalCodeSelect"/span>> 國家代碼</label>
</div>/span>
<form>
< input maxlength="20" name="BillIdentityMode"/span> id="BillIdentity" class="w-100 text-center" type="text" placeholder="insert billing id" />
</form>
<iframe name="sif1" sandbox="allow-form allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以使用.rules("add", options)并根據你應用.rules的選擇器來添加新規則:
$("input[type=radio]")。 prop("checked", false) 。
$("input[type=radio]:nth-child(1)").prop("checked", true)。
$("input[type=radio]").click(function (event) {
$("label.error"/span>).remove()。
$("input").removeClass('error') 。
$("#BillIdentity").val('')。
$("input[type=radio]").prop("checked", false) 。
$(this).prop("checked", true) 。
var id = $(this).attr("id") 。
console.log(id)。
if (id == "BillIdentitySelect") {
$("#BillIdentity").attr("maxLength"/span>, "30")
$("#BillIdentity").attr("placeholder", "插入帳單id")
$("#BillIdentity").attr("my-attr","billMode")
$("#BillIdentity[my-attr='billMode']").rules("add",{
required: true,
minlength: 11,
messages:{required:"required",minlength:"You must enter at least 11 characters" } 。
})
}
else if (id == "NationalCodeSelect") {
$("#BillIdentity").attr("maxLength", "10" )
$("#BillIdentity").attr("placeholder", "inser national code")
$("#BillIdentity").attr("my-attr","nationalCode") 。
$("#BillIdentity[my-attr='nationalCode']").rules("add",{
required: true,
minlength: 10,
messages:{required:"required",minlength:"The number is wrong!" }.
})
}
//event.preventDefault(); })
});
var $form = $("form"/span>);
$form.validate()。
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>/span>
<script src="https://cdnjs.cloudflare. com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>/span>
<div class="radioGroup">/span>
< input type="radio" id="BillIdentitySelect" name="radio1" />
<label for="BillIdentitySelect"/span>> 賬單ID</label>
< input type="radio" id="NationalCodeSelect" name="radio2" />
<label for="NationalCodeSelect"/span>> 國家代碼</label>
</div>/span>
<form>
< input maxlength="20" name="BillIdentity" id="BillIdentity" class="w-100 text-center" type="text" placeholder="insert billing id" />
</form>
<iframe name="sif2" sandbox="allow-form allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/311781.html
標籤:
下一篇:Java驗證欄位的條件是類的水平
