如何使用以下方法排除只读字段:在jquery中输入?

时间:2010-04-11 13:02:06

标签: jquery forms readonly clear

到目前为止,我只使用了一些基本的jquery选择器和函数。 但我正在看这个clear form function,我无法弄清楚如何添加它,这样我就可以删除隐藏的输入和读取后的只读输入。

任何人都可以帮忙吗? 感谢。

function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

2 个答案:

答案 0 :(得分:13)

如果元素的声明中包含readonly属性,则可以使用jQuery’s :not() selector

$(':input:not([readonly])', form)

否则使用以下内容过滤只读元素:

$(':input', form).each(function() {
    if (this.readOnly) return;
    // …
});

答案 1 :(得分:1)

<script type="text/javascript" language="JavaScript">
    jQuery(document).ready(function(){
        try {
            var t = jQuery("input[type=text]").not("[readonly]").focus(function(){
                jQuery(this).blur();
            });
            alert(t.length);
        }
        catch(error){
            alert("error :" + error);
        }
    });
</script>