id是正确的,但没有价值

id is right but not value

本文关键字:有价值 id      更新时间:2024-06-01

我试图提醒一个按钮的id(使用jquery生成),但当我提醒它的值时,它没有正确。代码在这里

function removeimg(name,btn12){        
        //make request to remove the name
       // $("#" + name).closest('li').remove();
      //  $("#" + btn12).remove();
       // document.getElementById(name).style.display = 'none';
       // document.getElementById(btn12).style.display = 'none';
       var str = "#" + btn12;
       alert(str);
       alert($(str).val());

    }

这是链接

http://shri-ram.lifekloud.com/pilot/step4.php

当您在选项卡"添加-删除照片"下上传图像时,按钮将生成

我正试图提醒id有一个按钮

val()不获取元素的id;val返回value元素。

要获取元素的id,请使用attr

alert($(str).attr('id'));

你的评论well its not even returning value thts the issue. but the id name is getting displayed correctly 只是在黑暗中捅了一刀

如果你有

<input type='button' id='b' value='btn' />

然后

alert($('#b').val()); 

实际上将显示CCD_ 4。也就是说,如果你有

<button id='b'>btn</button>

则将不显示任何内容。但正如我所说,这只是暗箭伤人。没有可用的html是不可能知道得更好的(恐怕我没有时间解析你的网站)

您的id #btnheader-8878374.png中有一个元字符.,这就是问题所在。

像这个一样逃跑

$('.#btnheader-8878374''.png')

试着让你的概念发挥作用。

完整代码,

var str = "#" + btn12;
str = str.replace('.','''''');
alert($(str).val());

您的问题很可能是按钮上没有设置value属性,因此调用val()不会返回任何结果。

如果您想要按钮的文本,只需调用text()即可。

参见jsFiddle


HTML

<button id="btn12">Button 12</button>



JQUERY

var str = "#" + "btn12";
alert( str );             // yields #btn12
alert( $(str).val() );    // yields nothing
alert( $(str).text() );   // yields Button 12