Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中获取html页面的id?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在jQuery中获取html页面的id?

Javascript 在jQuery中获取html页面的id?,javascript,jquery,html,Javascript,Jquery,Html,我在我的页面上有一个id,通过在变量中使用.html()函数获取所有html。现在,我想使用存储html的变量的id。在jQuery中如何实现这一点?我不知道。请帮帮我 var idhtml= $("#id").html(); 像这样,我有idhtml中的html <input type="text" id="id1" name="id1" value="" class ="test" /> <input type="text" id="id2" name="id1" val

我在我的页面上有一个id,通过在变量中使用
.html()
函数获取所有html。现在,我想使用存储html的变量的id。在jQuery中如何实现这一点?我不知道。请帮帮我

var idhtml= $("#id").html();
像这样,我有idhtml中的html

<input type="text" id="id1" name="id1" value="" class ="test" />
<input type="text" id="id2" name="id1" value="" class ="test" />
<input type="text" id="id3" name="id1" value="" class ="test" />


通过
idhtml
,我想获得变量中的id。

您可以获得如下id:

$("input[id^='id']").each(function () { 
    console.log(this.id); 

    // Change the id:
    $(this).attr("id", "new_id");
});
选择id以
id
开头的每个
input
,对于返回的每个输入,在控制台中打印其id。

尝试此操作

  $(document).ready(function(){
        var formId = $('form').attr('youId');
    });
您可以使用
.each()

设置可以使用的属性

1)
.attr('attribute\u name','attribute\u value')

2)
.data('attribute\u name','attribute\u value')

使用合适的选择器

 $("[name='id1']").each(function () { 
     console.log($(this).attr(id));
     //to set the attr you can use
     $(this).attr('attribute_name','attribute_value');
     //to set custom attribute, use .data()
     $(this).data('attribute_name','attribute_value');
 });

呃,什么?阿迪内奥我在如何访问这些id时没有出错。请告诉我know@GauravPandey:您想要哪些id以及您想要访问textbox值的位置??实际上我想要在id$(“输入[id^='id'])上设置attr。每个(函数(){$(this.attr('readonly',true);});Try:
$(this.prop('readonly',true)。如果它不起作用,那就是另外一回事了。确保选择器返回输入。在控制台中,它正在控制台中打印输入类型HTMLHIS