javascript - Jquery ID selector when attribute name has a hash - Stack Overflow

I'm running into a problem, our internal framework has a system where if you have a form & the

I'm running into a problem, our internal framework has a system where if you have a form & the containing element his name starts with a hash-tag it will do something when processing these elements later on.

So I have for instance :

<label width='auto' for='x_test' >Test:</label>
    <input type="checkbox" name="#x_test#" id="x_test" value="1"  />

now using Jquery if I want to select this input, for instance to hide it:

$( "#x_test" ).hide();

This does not work unless I remove the hashtags from the name of the element. But I'm doing a select by ID I'm not sure why this is such an issue.

Is this a known issue & is there something I can do?

We are on jquery-1.3.2

Thanks

UPDATE

Thanks for all the hints, it helped me a bit and I did learn some things from them but in the end the problem was bad existing code from someone else interfering with what I was trying to do.

We have a wizard en in each step it copies the inputs from that page to a hidden dynamic form for this wizard.

But they were also copying the id-attribute etc so this didn't respect the rule of unique id's anymore. Because of this JQuery / JQuery UI and all my JavaScript were behaving realy weird ofcourse. I ended up rewriting this wizard-thing so my JQuery etc do work.

I'm running into a problem, our internal framework has a system where if you have a form & the containing element his name starts with a hash-tag it will do something when processing these elements later on.

So I have for instance :

<label width='auto' for='x_test' >Test:</label>
    <input type="checkbox" name="#x_test#" id="x_test" value="1"  />

now using Jquery if I want to select this input, for instance to hide it:

$( "#x_test" ).hide();

This does not work unless I remove the hashtags from the name of the element. But I'm doing a select by ID I'm not sure why this is such an issue.

Is this a known issue & is there something I can do?

We are on jquery-1.3.2

Thanks

UPDATE

Thanks for all the hints, it helped me a bit and I did learn some things from them but in the end the problem was bad existing code from someone else interfering with what I was trying to do.

We have a wizard en in each step it copies the inputs from that page to a hidden dynamic form for this wizard.

But they were also copying the id-attribute etc so this didn't respect the rule of unique id's anymore. Because of this JQuery / JQuery UI and all my JavaScript were behaving realy weird ofcourse. I ended up rewriting this wizard-thing so my JQuery etc do work.

Share Improve this question edited Jul 6, 2015 at 10:23 gotjee asked Jul 2, 2015 at 7:59 gotjeegotjee 432 silver badges9 bronze badges 2
  • 2 jQuery 1.3.2 is very old. – Ram Commented Jul 2, 2015 at 8:03
  • 1 Your issue isn't reproducible: jsfiddle/qm0zecsd -- Seems to work as expected. – George Commented Jul 2, 2015 at 8:06
Add a ment  | 

5 Answers 5

Reset to default 3

To select by attribute:

$('[name="#x_test#"]').hide();

To select by id:

$('#x_test').hide();

or

$('[id="x_test"]').hide();

Check the below code (with jQuery 1.3.2).

The id selection applies a green colour, instead the name attribute selection applies a red colur:

$('#test').css({'color':'green'});
$('[name="#test"]').css({'color':'red'});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<div id="test">test id</div>
<div name="#test">name test</div>

Use attribute equals selector:

$("[name='#x_test#']").hide();

if usage of # will not be working for your framework, try attaching the id name to class and use class selector in jQuery

i.e Have

<label width='auto' for='x_test' >Test:</label>
    <input type="checkbox" name="#x_test#" id="x_test" class="x_test"  value="1"  />

and then use

$( ".x_test" ).hide();

this will works fine for me.

<script src="https://ajax.googleapis./ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<label width='auto' for='x_test' >Test:</label>
<input type="checkbox" name="#x_test#" id="x_test" value="1"  />
<script>
    $(document).ready(function () { 
    $( "#x_test" ).hide();    
});
</script>

Try following

$("input[name='#x_test#']").hide();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745218547a4617138.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信