Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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
试图通过jquery选择器从最接近LI标记的隐藏字段中提取值_Jquery - Fatal编程技术网

试图通过jquery选择器从最接近LI标记的隐藏字段中提取值

试图通过jquery选择器从最接近LI标记的隐藏字段中提取值,jquery,Jquery,当用户单击btnShipNow按钮时,我试图捕获隐藏字段hdnServiceCode中存储的值。所以这里我粘贴了一个示例html。我想当用户点击btnShipNow按钮时,我想从hdnServiceCode隐藏字段中提取最接近btnShipNow按钮的值 这是我的html 但是上面的代码不起作用。我犯错误的地方。请帮助我从最靠近btnShipNow按钮的隐藏字段中捕获值。谢谢你 alert($(this).closest('#rec').find("#hdnServiceCode").val(

当用户单击
btnShipNow
按钮时,我试图捕获隐藏字段
hdnServiceCode
中存储的值。所以这里我粘贴了一个示例html。我想当用户点击
btnShipNow
按钮时,我想从
hdnServiceCode
隐藏字段中提取最接近
btnShipNow
按钮的值

这是我的html 但是上面的代码不起作用。我犯错误的地方。请帮助我从最靠近
btnShipNow
按钮的隐藏字段中捕获值。谢谢你

 alert($(this).closest('#rec').find("#hdnServiceCode").val());
可能你的选择有很多错误,比如

  • 误用id选择器
  • 多次不必要地使用最近的
但主要应该为DOM中的元素使用唯一ID。为当前具有id
hdnServiceCode
的元素使用一个类,并使用

 alert($(this).closest('#rec').find(".hdnServiceCode").val());
这是一把小提琴:

我用匹配的ID解决了这个问题,并使用了以下方法:

$('.nobutton').click(function(){
    alert($(this).prev().text());
});

As id应该是唯一的。因此,修改您的代码并在字段中添加classhdn

<div id="rec">
<ul>
    <li>
        <input type="hidden" value="65" id="hdnServiceCode1" class="hdn"/>
    UPS Express Saver</li>
</ul>
<ul>
    <li>24.47&nbsp;GBP</li>
    <li class="nobutton"><span id="rptPrice_ctl01_btnShipNow">Ship Now</span>
    </li>
</ul>
</div>
<div id="rec">
<ul>
    <li>
        <input type="hidden" value="15" id="hdnServiceCode2" class="hdn"/>
    UPS Express Saver</li>
</ul>
<ul>
    <li>11.47&nbsp;GBP</li>
    <li class="nobutton"><span id="rptPrice_ctl01_btnShipNow">Ship Now</span>
    </li>
</ul>

您的ID应该是唯一的。ID必须是唯一的,您可以在li元素的单击处理程序中使用$(this).prev(),或者在范围中使用$(this).parent().prev(),以获得“立即发货”按钮之前的li元素。谢谢回复。如果我需要检索文本UPS Express Saver或UPS Express Saver等,那么我还需要添加哪些代码?如果我只需要从li标记而不是html中提取文本,那么我如何才能做到这一点?我需要提取UPS Express Saver唯一的文本值…那么如何在代码中继续???@Thomas
$(this).closest('ul')。prev('ul')。find('li')。text()
@Thomas很乐意帮助。>!
$('.nobutton').click(function(){
    alert($(this).prev().text());
});
<div id="rec">
<ul>
    <li>
        <input type="hidden" value="65" id="hdnServiceCode1" class="hdn"/>
    UPS Express Saver</li>
</ul>
<ul>
    <li>24.47&nbsp;GBP</li>
    <li class="nobutton"><span id="rptPrice_ctl01_btnShipNow">Ship Now</span>
    </li>
</ul>
</div>
<div id="rec">
<ul>
    <li>
        <input type="hidden" value="15" id="hdnServiceCode2" class="hdn"/>
    UPS Express Saver</li>
</ul>
<ul>
    <li>11.47&nbsp;GBP</li>
    <li class="nobutton"><span id="rptPrice_ctl01_btnShipNow">Ship Now</span>
    </li>
</ul>
$('.nobutton').on('click', function () {
        alert($(this).closest('#rec').find(".hdn").val());
    alert($(this).closest('#rec').find("ul:eq(0)  li").text());
    });