我正在学习jquery,我遇到了问题.
这是代码
$(function(){
var gotProducts=new Array();
var productsWithDelete=new Array();
$('.addProducts').on('keyup',function(event) {
var searchVal=$(this).val().trim();
if(searchVal.length > 0) {
$.ajax({
url: 'http://localhost/url',
data: { products: $(this).val(), },
type: 'POST',
dataType: 'html',
success: function(msg) {
$('#printTheProducts').html(msg);
}
});
}
});
$('.productsButton').click(function() {
alert('yes');
});
});
Run Code Online (Sandbox Code Playgroud)
我从ajax调用获得的响应是一个具有类productsButton的按钮.
现在,当我尝试单击该按钮时,我通过ajax然后它不会提示是.我的意思是它什么也没做.问题: -
可能是什么问题?
我正在尝试使用jQuery Validation插件验证我的表单.
这是代码
$(document).ready(function(){
var productsForm=$('#products-form');
productsForm.validate({
//debug:true,
invalidHandler: function(event, validator) {
// 'this' refers to the form
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'You missed 1 field. It has been highlighted'
: 'You missed ' + errors + ' fields. They have been highlighted';
$("div.error span").html(message);
$("div.error").show();
} else {
$("div.error").hide();
}
},
rules:{
productName: {
required: true,
minlength:2,
//here i tried to create a function
onfocusout: function(element){
var myValue=$(element).val();
if(myValue.match(/[<>$]/)) …Run Code Online (Sandbox Code Playgroud) 目前我有这样的数组
$myAllProducts= array(
0=> array(
'productName' => "Classmate",
'productId' => 2
),
1=>array(
'productName' => "tata",
'productId' => 3
),
2=>array(
'productName' => "abcd",
'productId' => 4
),
1=>array(
'productName' => "pen",
'productId' => 7
),
)
Run Code Online (Sandbox Code Playgroud)
我想将它转换为类似的数组
array('Classmate'=>2,
'tata'=>3,
'abcd'=>4,
'pen'=>7)
Run Code Online (Sandbox Code Playgroud)
我是这样做的
$productList=array();
foreach($myAllProducts as $record)
{
$productsList[$record['productName']]=$record['productId'];
}
Run Code Online (Sandbox Code Playgroud)
问题: - 尽管我使用我的循环成功获得了所需的结果,但我想知道是否可以使用任何内置函数或以更好的方式完成它?
我对jquery不太满意,因此需要你的帮助.
我有一个表单,其中有两个字段"origin"和"destination".所以我在两个字段中使用jquery自动完成.
$('#form1_origin,#form1_destination').autocomplete({
lookup: countriesArray,
minChars: 0,
});
Run Code Online (Sandbox Code Playgroud)
它工作得很好但是当我尝试使用jquery验证插件验证这些字段时,我遇到了一些问题.
这是代码: -
$('#form_1').validate({
errorClass: "airError",
focusInvalid: true,
success: function(element){
$(element).closest('.airError').removeClass('.airError');
},
errorPlacement: function(error,element){
if(element.attr("name") === "form1_infant" || element.attr("name") === "form1_departure"){
error.insertAfter('#showErrors');
}else{
error.insertAfter(element);
}
},
rules: {
form1_origin: {
validSelectionOrigin: true,
},
form1_destination:{
validSelectionDestination: true,
compare_origin_dest: true
},
// rules for rest of the fields
},
});
Run Code Online (Sandbox Code Playgroud)
问题:-
我很确定jquery会验证onkeyup上的字段.所以,如果我TYPE在此字段中的值,它工作正常,但是当我选择从自动完成列表中的值,它不验证领域,因为我仍然可以看到正在显示的错误.那么我能做些什么来使它适用于两者?