簡體   English   中英

我必須在運行時使用 jquery 將輸入類型從 html 表單的文本更改為 tel

[英]I have to change the input type to tel from text of the html form on runtime using jquery

我對 jQuery 很陌生。 我有 HTML 表單,當用戶使用 jquery 單擊輸入框時,我想將輸入類型從文本更改為電話。 還有許多輸入類型為文本的字段(創建 aem 組件),甚至 class 的名稱也會相同,因此,該表單元素的選擇應該是模式 =“[+0-9()-]*”。

<form class="form">
    <input type="text" name="myPassword" pattern="[+0-9()-]*"/>
    <input type="text" name="myPassword" />
</form>
$("button").click(function() {
    $('.form').find(pattern:'[+0-9()-]*').each(function() {
       $("<input type='tel' />").attr({ name: this.name, value: this.value }).insertBefore(this);
    }).remove();
});

output 應該是當用戶只需單擊輸入字段時,輸入類型應更改為“tel”。

我不確定我對你的問題的理解,但這里有一個關於如何做的提示

$("button").click(function() {
  $('.form').find('[name="myPassword"]').each(function() {
    $(this).attr('type', 'tel');
  });
});

問題出在 find() 部分。 這是正確的語法:

$("button").click(function() {
    $(".form").find("input[pattern='[+0-9()-\]*']").each(function() {
       $("<input type='tel' />").attr({ name: this.name, value: this.value }).insertBefore(this);
    }).remove();
});

現在關於您在評論中所說的話,我認為可以以更簡單的方式完成。 用模式[+0-9()-\]*的輸入上的點擊偵聽器替換洞的東西,並在點擊它們時將其類型更改為tel

$("input[pattern='[+0-9()-\]*']").click(function() {
    $(this).attr("type", "tel");
});

另外,我不知道您是否真的需要等待輸入被點擊,但如果不是,您可以在頁面加載時通過在文檔准備好時運行它來簡單地更改它們的類型:

$("input[pattern='[+0-9()-\]*']").attr("type", "tel");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM