简体   繁体   English

jQuery输入属性从x更改为y

[英]jQuery input attribute change from x to y

I try to replace the type of a input through jquery from "date_calendar" to "date". 我尝试通过jquery将输入的类型从“date_calendar”替换为“date”。 The input's name is "DateOfBirth[0]", but for some reason it doesn't work with the code bellow.. 输入的名称是“DateOfBirth [0]”,但由于某种原因,它不适用于下面的代码。

I am new to jquery/javascript and maybe I am missing something.. How can I do it? 我是jquery / javascript的新手,也许我错过了什么......我怎么能这样做? What did I missed? 我错过了什么? This is the current code: 这是当前的代码:

$(document).ready(function() {
$('input[name=DateOfBirth[0]]').attr('type', 'date');
});

HTML: HTML:

<input min="1938-04-28" max="1995-04-28" class="form_input_date" type="date_calendar" name="DateOfBirth[0]">

Thank you very much.. 非常感谢你..

Edit: I tried the double backslash [name=DateOfBirth\\\\[0\\\\]] and to quote the name value [name="DateOfBirth[0]"] but it still doesn't work.. 编辑:我尝试了双反斜杠[name=DateOfBirth\\\\[0\\\\]]并引用名称值[name="DateOfBirth[0]"]但它仍然不起作用..

Simply do this: 只需这样做:

$('input[name="DateOfBirth[0]"]').attr('type', 'date');
  • You need to double quotes the name here. 你需要在这里双引号。
  • Without quotes, you will get error like: 如果没有引号,您将收到如下错误:
    • Syntax error, unrecognized expression: input[name=DateOfBirth[0]]

FIDDLE 小提琴

You can't change the type property of an input using jQuery, because IE doesn't support it. 您不能使用jQuery更改输入的type属性,因为IE不支持它。

change type of input field with jQuery 使用jQuery更改输入字段的类型

You need to destroy the input and make a new one instead. 您需要销毁输入并改为创建新输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM