仅针对输入类型文本或电话,而不是收音机或复选框(on focus only for input type text or tel, not radio or checkboxes)

我正在使用此代码自动滚动到所选的输入类型:

$( document ).on( "focus", "input", function() { if(document.activeElement.tagName=="INPUT"){ window.setTimeout(function(){ document.activeElement.scrollIntoView(); },0); } return false; });

问题是,我想排除单选按钮和复选框,并且只用于输入类型tel或text 。 我如何实现这一目标?

I am using this code to auto-scroll to the selected input type:

$( document ).on( "focus", "input", function() { if(document.activeElement.tagName=="INPUT"){ window.setTimeout(function(){ document.activeElement.scrollIntoView(); },0); } return false; });

The problem is, I want to exclude radio buttons and checkboxes ..and only use it for input type tel or text. How do I achieve that?

最满意答案

您只能根据类型选择相关输入,而不是选择所有输入标记:

$( document ).on( "focus", "input[type='tel'],input[type='text']", function() {
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" /> 
  
 

另一种选择是检查焦点元素的type ,并在选定类型的情况下中断该函数的运行:

$( document ).on( "focus", "input", function() {
  if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
    return;
  }
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" /> 
  
 

You can select only the relevant inputs, based on their type, instead of select all of the input tags:

$( document ).on( "focus", "input[type='tel'],input[type='text']", function() {
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" /> 
  
 

Another option is to check the type of the focused element, and break the running of the function in case of a selected types:

$( document ).on( "focus", "input", function() {
  if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
    return;
  }
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" /> 
  
 

仅针对输入类型文本或电话,而不是收音机或复选框(on focus only for input type text or tel, not radio or checkboxes)

我正在使用此代码自动滚动到所选的输入类型:

$( document ).on( "focus", "input", function() { if(document.activeElement.tagName=="INPUT"){ window.setTimeout(function(){ document.activeElement.scrollIntoView(); },0); } return false; });

问题是,我想排除单选按钮和复选框,并且只用于输入类型tel或text 。 我如何实现这一目标?

I am using this code to auto-scroll to the selected input type:

$( document ).on( "focus", "input", function() { if(document.activeElement.tagName=="INPUT"){ window.setTimeout(function(){ document.activeElement.scrollIntoView(); },0); } return false; });

The problem is, I want to exclude radio buttons and checkboxes ..and only use it for input type tel or text. How do I achieve that?

最满意答案

您只能根据类型选择相关输入,而不是选择所有输入标记:

$( document ).on( "focus", "input[type='tel'],input[type='text']", function() {
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" /> 
  
 

另一种选择是检查焦点元素的type ,并在选定类型的情况下中断该函数的运行:

$( document ).on( "focus", "input", function() {
  if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
    return;
  }
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" /> 
  
 

You can select only the relevant inputs, based on their type, instead of select all of the input tags:

$( document ).on( "focus", "input[type='tel'],input[type='text']", function() {
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" /> 
  
 

Another option is to check the type of the focused element, and break the running of the function in case of a selected types:

$( document ).on( "focus", "input", function() {
  if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
    return;
  }
  console.log('relevant element focused');
}); 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" />