How do i disable a text box within an iframe

依然范特西╮ 提交于 2019-12-11 00:56:43

问题


The scenario: One big bulky page called index.html, everything happens here. Index has 10 tabs (jquery ui tabs). Using jquery ui's accordion widget, each tab now has, on average, 3-5 sub categories (I was denied posting a picture as spam prevention but I hope you understand what the layout looks like). Here's where the problem starts. An iframe resides in all of these, so I'm looking at 25-30 iframes with about 5-10 text fields in each iframe. What I want to do is just simply enable/disable these text fields.

Quite simply i know this can be done by:

$('input[type=text]').attr("disabled", "disabled");

$('input[type=text]').removeAttr("disabled")

A button on index triggers a function:

 $(".frameClass").each(function () {
                console.log("i found a frame called " + this.name);
});

How do I make this connection for all input text fields to be enabled on a click? I even tried creating a function within the iframe and tried calling it from within the .each()

this.unlockFields();

hasn't gotten me anywhere, any thoughts?


回答1:


You have to use .contents() to access the elements in the iframe, like so:

$(".frameClass").each(function () {
     $(this).contents().find('input[type=text]').attr('disabled', 'disabled');
});

that should do what you want, given that the iframe contains a page that is on the same domain as the main page.




回答2:


Try this:

var iframes = $('iframe');
iframes.each(function(index, iframe) {
    var iframe_content = ($.browser.msie ? $(iframe.contentWindow.document) : $(iframe.contentDocument));
    iframe_content.find('input[type=text]').attr({disabled:'disabled'});
});


来源:https://stackoverflow.com/questions/12901452/how-do-i-disable-a-text-box-within-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!