最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery - Disable Button and Input with dynamic names - Stack Overflow

programmeradmin2浏览0评论

I have the a table structure similar to this, but a lot of other nested stuff above and below. I need to disable the button and link (or possibly change the button visibility, instead of disabling it?).

The button ID and anchor names are dynamic (value 10, below) and I've tried everything I can think of to grab these elements from the table and disable, but nothing seems to work:

<tr>
<td class="t-last">
<button id="btnEdit10" class="ui-state-active ui-corner-all button-outer"    
onclick="ClientValues(10);">Edit</button>
<a name="lnkDelete10" class="cursor_hand" onclick="DeleteIt(10,200);">
<u>Delete</u>
</a>
</td>
<tr>
<td class="t-last">
<button id="btnEdit17" class="ui-state-active ui-corner-all button-outer"    
onclick="ClientValues(17);">Edit</button>
<a name="lnkDelete17" class="cursor_hand" onclick="DeleteIt(17,200);">
<u>Delete</u>
</a>
</td>

I've guessing regular expressions will be my best bet, but I still haven't gotten it to work. This is what I'm trying to use to disable the button, but I can't access it:

$('btnEdit10').attr("disabled", true); //after looping to the control

Any help would be greatly appreciated!

EDIT: I wasn't very clear in my question. I'm trying to disable every button that starts with "btnEdit" and every link that starts with "lnkDelete".

I have the a table structure similar to this, but a lot of other nested stuff above and below. I need to disable the button and link (or possibly change the button visibility, instead of disabling it?).

The button ID and anchor names are dynamic (value 10, below) and I've tried everything I can think of to grab these elements from the table and disable, but nothing seems to work:

<tr>
<td class="t-last">
<button id="btnEdit10" class="ui-state-active ui-corner-all button-outer"    
onclick="ClientValues(10);">Edit</button>
<a name="lnkDelete10" class="cursor_hand" onclick="DeleteIt(10,200);">
<u>Delete</u>
</a>
</td>
<tr>
<td class="t-last">
<button id="btnEdit17" class="ui-state-active ui-corner-all button-outer"    
onclick="ClientValues(17);">Edit</button>
<a name="lnkDelete17" class="cursor_hand" onclick="DeleteIt(17,200);">
<u>Delete</u>
</a>
</td>

I've guessing regular expressions will be my best bet, but I still haven't gotten it to work. This is what I'm trying to use to disable the button, but I can't access it:

$('btnEdit10').attr("disabled", true); //after looping to the control

Any help would be greatly appreciated!

EDIT: I wasn't very clear in my question. I'm trying to disable every button that starts with "btnEdit" and every link that starts with "lnkDelete".

Share Improve this question edited Mar 10, 2013 at 20:33 Justin asked Mar 10, 2013 at 20:13 JustinJustin 3791 gold badge6 silver badges14 bronze badges 5
  • Are you trying to disable everything that starts with btnEdit, or just btnEdit10, and if so, where does 10 e from ? – adeneo Commented Mar 10, 2013 at 20:23
  • I'm trying to disable everything that starts with "btnEdit" or, for the links, starts with "lnkDelete". I'd like to then loop through and disable them all (both links and buttons). – Justin Commented Mar 10, 2013 at 20:31
  • 1 That's what I figured, was waiting for a response to post an answer for like 15 minutes? Looks like those buttons belong to jQuery UI, and if so they are possibly dynamic, which means they don't exist on pageload. After the buttons are added to the DOM, my answer below should work. – adeneo Commented Mar 10, 2013 at 20:40
  • You are correct, they're from jQuery UI. I learned quite a few things from you today Adeneo -- you are awesome! ...and have no idea how much you saved my arse! – Justin Commented Mar 11, 2013 at 0:30
  • adeneo -- Do you have any more information (a link perhaps) on jQuery UI and it's interaction and load in relation to the DOM? I'm doing research now b/c it's already causing other problems. Thanks again! – Justin Commented Mar 27, 2013 at 21:11
Add a ment  | 

3 Answers 3

Reset to default 4

If I understand it well, something like this

 $('#btnEdit' + dynamic_part).attr("disabled", disable);

Update:

you can select all ids start with btnEdit like this

 $("[id^=btnEdit]").attr("disabled", disable);

To disable everything that starts with "btnEdit", use the "attributes starts with" selector :

$('[id^="btnEdit"]').prop("disabled", true);

or for both:

$('[id^="btnEdit"], [id^="lnkDelete"]').prop("disabled", true);

EDIT:

or maybe:

$('[id^="btnEdit"]').prop("disabled", true);
$('[id^="lnkDelete"]').attr("onclick", "");

If you just want to disable a specific button, then you can do something like this...

$('#btnEdit10').attr("disabled", disable);

Otherwise if you want control a group of buttons to disable, you can do what InTry mentioned and setup a variable next to the selector name...

$('#btnEdit' + my_number).attr("disabled", disable);

and store the appropriate number value(s) into that variable (my_number) with some simple coding.

发布评论

评论列表(0)

  1. 暂无评论