Discuss / JavaScript / 我来试试

我来试试

Topic source

渡口风沙

#1 Created at ... [Delete] [Delete and Lock User]
json={};
$('#test-form :input[type!=submit]').each(function(){
        if (this.type==="radio"&&this.checked){
            json[this.name]=this.value;
        }else{
            json[this.name]=this.value;
        }
    });
json = JSON.stringify(json);

$('#test-form :input[type!=submit]')和$('#test-form input[type!=submit]')第二段代码为什么获取到的元素会少一个option?我感觉这两段代码是差不多的意思啊,第一个是在表单里获取input元素并且过滤type=submit的input,第二个也是在表单里获取input元素并且过滤type=submit的input,只不过一个用到过滤功能一个用到层级功能,为什么第二就是少获取到option元素?而且为什么获取input的元素,第一个会选择到option?

渡口风沙

#3 Created at ... [Delete] [Delete and Lock User]

不好意思,你说的我不太理解;

我上面的代码有bug。 更正后如下:

json={};
$('#test-form :input[type!=submit]').each(function(){
        if (this.type!="radio"){
           json[this.name]=this.value;            
        }else if(this.checked){
           json[this.name]=this.value;
        }
    });
json = JSON.stringify(json);

$('#test-form :input[type!=submit]')和$('#test-form input[type!=submit]') 第一种是在input的范围内取得type!=submit的dom,第二个是在test-form的范围内取得type!=submit的dom,是过滤器的方式。 另外针对表单jquery也有 :input的选择方式可以选择可以选择<input>,<textarea>,<select>和<button>;


  • 1

Reply