jq 一些验证表单的常用方法

写了一下午的jq 快崩溃了。。记录一下

jquery通过相同的class获取元素并遍历取值

	$(".cartProList span.proPrice").each(function(){
		// let this = $(this);
		console.log($(this).html());
	});

JS判断字符串变量是否含有某个字串的实现方法

JS判断字符串变量是否含有某个字串的实现方法

1

2

3

4

varCts = "bblText";

if(Cts.indexOf("Text") > 0 ){

alert('Cts中包含Text字符串');

}

indexOf用法:

返回 String 对象内第一次出现子字符串的字符位置。

strObj.indexOf(subString[, startIndex])

参数

strObj

必选项。String 对象或文字。

subString

必选项。要在 String 对象中查找的子字符串。

starIndex

可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。

说明

indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。

如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

从左向右执行查找。否则,该方法与 lastIndexOf 相同。

示例

下面的示例说明了 indexOf 方法的用法。

1

2

3

4

5

function IndexDemo(str2){

var str1 = "BABEBIBOBUBABEBIBOBU"

var s = str1.indexOf(str2);

return(s);

}

对于JavaScript的indexOf忽略大小写

JavaScript中indexOf函数方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

indexOf函数是从左向右执行查找。否则,该方法与 lastIndexOf 相同。

下面的示例说明了indexOf函数方法的用法。

1

2

3

4

5

functionIndexDemo(str2){

varstr1 = "BABEBIBOBUBABEBIBOBU"

vars = str1.indexOf(str2);

return(s);

}

 jquery 按钮 select 不能点击

$("input").attr("disabled", true); 或者$("input").attr("disabled", "disabled");

jQuery 实现表单提交前检验元素值是否为空

在<form>标签中添加 onsubmit="return checkForm()"

jQuery中each的用法之退出循环和结束本次循环

jQuery中each类似于javascript的for循环 
但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用return,
break           用return false
continue      用return ture

如何用jQuery获得select的值

如何用jQuery获得select的值,在网上找了看了一下,下面将总结一下:

1.获取第一个option的值       

 $('#test option:first').val();

2.最后一个option的值                     

$('#test option:last').val();

3.获取第二个option的值          

$('#test option:eq(1)').val();

4.获取选中的值                         

$('#test').val();

$('#test option:selected').val();

5.设置值为2的option为选中状态   

$('#test').attr('value','2');

6.设置最后一个option为选中

$('#test option:last').attr('selected','selected');

$("#test").attr('value' , $('#test option:last').val());

$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());

7.获取select的长度            

$('#test option').length;

8.添加一个option

$("#test").append("<option value='n+1'>第N+1项</option>");

$("<option value='n+1'>第N+1项</option>").appendTo("#test");

9.添除选中项       

 $('#test option:selected').remove();

10.删除项选中(这里删除第一项)       

$('#test option:first').remove();

11.指定值被删除

复制代码

$('#test option').each(function(){

   if( $(this).val() == '5'){

        $(this).remove();
    }
});

$('#test option[value=5]').remove();

复制代码

12.获取第一个Group的标签

$('#test optgroup:eq(0)').attr('label');

13.获取第二group下面第一个option的值

$('#test optgroup:eq(1) : option:eq(0)').val();

14.根据option的值选中option

$("#sel option:contains('C')").prop("selected", true);

jQuery 判断是否为数字的方法 及 转换数字函数

1

2

3

4

5

6

7

8

<script language="javascript">

var t=$("#id").val();//这个就是我们要判断的值了

if(!isNaN(t)){

  alert("是数字");

}else{

  alert("不全是数字");

}

</script>

猜你喜欢

转载自blog.csdn.net/BinGuoLA/article/details/81566457